3

我有一组样式和刷子在我加载作为MergedDictionary在我的前级控制的XAML ResourceDictionary中定义:更换出现合并资源字典上的Silverlight应用程序的负载

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/MyAssembly;component/Styles.xaml"/> 
</ResourceDictionary.MergedDictionaries> 

我想到可选如果XAP中存在与自己的ResourceDictionary不同的XAML文件,则替换其中一些样式&画笔。我正在尝试在运行时在此字典中合并,然后在我的用户控件上调用InitializeComponent()。我使用下面的代码,试图做到这一点:

public static class StyleLoader 
{ 
    public static void MergeStyle(string xamlUri) 
    { 
     try 
     { 
      XDocument xaml = XDocument.Load(xamlUri); 
      ResourceDictionary rd = XamlReader.Load(xaml.ToString()) as ResourceDictionary; 
      Application.Current.Resources.MergedDictionaries.Add(rd); 

     } 
     catch (XmlException ex) 
     { 
      // if the file doesn't exist, we can't add it 
     } 
    } 
} 

从可选文件中的资源字典是加载罚款和合并,但是我原来的样式集似乎总是重写此。如果我在XAML注释掉合并字典,仅仅是为了在运行时加载它们它完美的作品:

StyleLoader.MergeStyle("/MyAssembly;component/Styles.xaml"); 
    StyleLoader.MergeStyle("BrushReplacements.xaml"); 

    InitializeComponent(); 

我的这个解决方案的问题是,没有在XAML的默认样式,我不能在Blend打开项目。任何人有任何想法的解决方案,将保持我的默认样式Blend但已允许我可以在运行时使用动态加载的资源字典选择性覆盖它们?谢谢!

回答

1

这里就是颜色/画笔应用,而不是直接指静态资源绑定的一个解决方案:
http://blogs.msdn.com/corrinab/archive/2009/11/24/9927729.aspx
第二部分:
http://blogs.msdn.com/corrinab/archive/2009/12/02/9931283.aspx

目前,我觉得这样的事情是最好的方法在运行时处理动态切换主题。但它确实需要很多工作来移植现有的应用程序才能使用这种机制。

+1

这不是我真正想做的事情(这看起来好像应该用资源来解决,而不是绑定画笔)......但它似乎是我迄今见过的动态样式的最佳解决方法所以我会将其标记为已回答。 – 2010-03-17 19:49:27

相关问题