在 Silverlight 应用程序加载时替换合并资源字典
我在 ResourceDictionary 中定义了一组样式和画笔,将其作为 MergedDictionary 加载到我的顶级控件的 XAML 中:
<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 知道我的默认样式,但允许我有选择地在运行时使用动态加载的资源字典覆盖它们?谢谢!
I have a set of styles and brushes defined in a ResourceDictionary that I am loading as a MergedDictionary in XAML of my top-level control:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyAssembly;component/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
I am trying to optionally replace some of these styles & brushes if a different XAML file exists in the XAP with its own ResourceDictionary. I am trying to merge in this dictionary at runtime before InitializeComponent() is called on my user control. I am using the following code to attempt to do this:
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
}
}
}
The resource dictionary from the optional file is loaded fine and merged, however my original set of styles always seems to be overriding this. If I comment out the merged dictionary in XAML and simply load them at runtime in order it works perfectly:
StyleLoader.MergeStyle("/MyAssembly;component/Styles.xaml");
StyleLoader.MergeStyle("BrushReplacements.xaml");
InitializeComponent();
My problem with this solution is that without the default styles in XAML, I can not open the project in Blend. Anyone have any ideas for a solution that will keep my default styles known to Blend but allow me to optionally override them at runtime with a dynamically loaded resource dictionary? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个解决方案,其中颜色/画笔通过绑定应用,而不是直接引用静态资源:
http://blogs.msdn.com/corrinab/archive/ 2009/11/24/9927729.aspx
第二部分:
http://blogs.msdn.com/corrinab/archive/ 2009/12/02/9931283.aspx
目前我认为这样的东西是处理运行时动态切换主题的最佳方法。但将现有应用程序移植到使用这样的机制确实需要做大量的工作。
Here is a solution where colors/brushes are applied with bindings instead of referring directly to the static resources:
http://blogs.msdn.com/corrinab/archive/2009/11/24/9927729.aspx
Part two:
http://blogs.msdn.com/corrinab/archive/2009/12/02/9931283.aspx
Currently I think something like this is the best way of dealing with dynamically switching themes at runtime. But it does require a lot of work to port an existing application to use a mechanism like this.