简体   繁体   English

多个资源词典,选择一个

[英]Multiple resource dictionaries, select one

I am working on an app that uses a resource dictionary for styling. 我正在开发一个使用资源字典进行样式化的应用程序。 I have to make a change that will enable a config setting to change the dictionary being used. 我必须进行更改,以使配置设置能够更改正在使用的字典。

I have three dictionaries. 我有三本词典。 Original.xaml, Neon.xaml & Graphite.xaml Original.xaml,Neon.xaml和Graphite.xaml

In the App.xaml I have.. 在App.xaml我有..

     <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Original.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

I can change the dictionary being used by calling.. 我可以通过调用来更改正在使用的字典..

        private void DynamicLoadStyles(string StyleToUse)
        {
        string fileName = "C:\\Data\\Projects\\MyApp\\MyApp\\Resources\\" + StyleToUse + ".xaml";

            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                ResourceDictionary dic =
                   (ResourceDictionary)XamlReader.Load(fs);
                Resources.MergedDictionaries.Clear();
                Resources.MergedDictionaries.Add(dic);
            }
         }

All works as expect (however I am not sure if this is the correct way to do it). 一切都按预期工作(但我不确定这是否是正确的方法)。 The problem is I would rather embed the files and not have to load them from an external file. 问题是我宁愿嵌入文件而不必从外部文件加载它们。

I have searched for info to help but cannot find what I am looking for. 我搜索了信息以帮助但找不到我要找的东西。 That said I am new to WPF (3 weeks) and not really sure what I am doing yet. 这就是说我是WPF的新手(3周)并且不确定我在做什么。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I don't really like to answer my own question as it suggests I shouldn't have asked in the first place. 我真的不想回答我自己的问题,因为它表明我不应该首先提出问题。 But I have solved the problem using .... 但是我用....解决了这个问题。

    private void LoadDynamicResource(String StyleToUse)
    {

        ResourceDictionary dic = new ResourceDictionary { Source = new Uri(StyleToUse, UriKind.Relative) };
        Resources.MergedDictionaries.Clear();
        Resources.MergedDictionaries.Add(dic);

    }

I would be interested in opinions on this though. 我对这个问题很感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM