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

标签 c# wpf xaml

我正在尝试实现 Style Binding from this article in 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 中绑定(bind)到的颜色/画笔。是否有可能获取资源实例键控“ThemeProvider”以便我可以更新它?

如果您了解跨平台 WPF 和 Silverlight 实现,则加分!

注意:我需要在声明 Generic.xaml 的程序集外部获取这个

最佳答案

如果您的资源是在 generic.xaml 中定义的,或者是在 App.xaml 的 MergedDictionaries 中定义的任何资源,那么您需要使用 Application.Current.Resources,例如:

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

关于c# - 以编程方式从 Generic.xaml 中查找资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14528622/

相关文章:

c# - AutoMapper 将 2 个表中的记录连接到单个 IEnumerable View 模型中

c# - 创建包含类列表的类列表

c# - 没有 MediaTypeFormatter 可用于从媒体类型为 'HttpRequestMessage' 的内容中读取类型为 'multipart/form-data' 的对象

wpf - 在 WPF 中使用 UWP 控件

c# - WPF AxisAngleRotation3D 与 XNA CreateFromAxisAngle

c# - 有没有办法在 Xamarin Forms 'Entry' XAML 上禁用自动大写

c# - ObjectStateManager 不包含引用类型对象的 ObjectStateEntry

wpf - 有什么方法可以阻止 WPF CheckBox 触发其 Checked 事件吗?

c# - 绑定(bind)标签和文本框(XAML)时的区别

xaml - Xamarin TapGestureRecognizer 有时无法正常工作