accessing a resource dictionary in code wpf

元气小坏坏 提交于 2019-12-03 23:08:09
Prince Ashitaka

What I use is with UriKind like

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

HTH

@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!