从Generic.xaml中以编程方式查找资源

4
我正在尝试在WPF和Silverlight中实现这篇文章中的样式绑定
我有一个资源字典generic.Xaml,其中包含以下代码:
<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="/AComponent;component/Themes/MyCustomStyles.xaml" />
   </ResourceDictionary.MergedDictionaries>

MyCustomStyles.xaml文件的开头如下所示

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   <t:ThemeColorProvider x:Key="ThemeProvider"/>

我需要获取ThemeProvider的实例,以更新在Generic.xaml中绑定的颜色/画刷。是否可以获取键为“ThemeProvider”的资源实例,以便我可以更新它?
如果您知道一个跨平台的WPF和Silverlight实现,将获得额外的信用分!
注意:我需要在声明Generic.xaml的程序集之外获取它。

使用FindResource方法怎么样? - Hossein Narimani Rad
2个回答

4
如果您的资源在generic.xaml中定义或在App.xaml中的MergedDictionaries中定义,则需要使用Application.Current.Resources,例如:

BackgroundColor = (Color)Application.Current.Resources["ApplicationBarBackgroundColor"]


(其中,“ApplicationBarBackgroundColor”是资源的键名)

谢谢 :) 我们最终使用自定义的MarkupExtension实现了这个。 - Dr. Andrew Burnett-Thompson

1
这可能有所帮助:

ThemeColorProvider value= (ThemeColorProvider)FindResource("ThemeProvider");
// update value

嗨,谢谢 - 但是FindResource在哪里?我需要从静态(非FrameworkElement)类中定位它。 - Dr. Andrew Burnett-Thompson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接