在 Silverlight 应用程序加载时替换合并资源字典

发布于 2024-08-16 00:59:56 字数 1300 浏览 10 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2024-08-23 00:59:56

这是一个解决方案,其中颜色/画笔通过绑定应用,而不是直接引用静态资源:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文