accessing a resource dictionary in code wpf

后端 未结 2 1217
时光说笑
时光说笑 2021-02-18 22:49

The same line of code in the same assembly works for one test fixture but not another. Here is the line of code:

var dic = new ResourceDictionary { Source = new          


        
相关标签:
2条回答
  • 2021-02-18 23:11

    @Prince Ashitaka answer tells you how to correct your URI

    However the preferred way of accessing a ResourceDictionary is that in XAML you add it on as a merged dictionary

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ImageResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    then you can access it via code using the TryFindResource(string Key) from any code behind file

    0 讨论(0)
  • 2021-02-18 23:29

    What I use is with UriKind like

    var resource = new ResourceDictionary
    {
        Source = new Uri("/myAssemblyName;component/Themes/generic.xaml",
                         UriKind.RelativeOrAbsolute)
    };
    

    HTH

    0 讨论(0)
提交回复
热议问题