C# 动态加载资源

匿名 (未验证) 提交于 2019-12-03 00:03:02

在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式,TextBlockStyle.xaml是一个ResourceDictionary,包含了所需样式

通过相对路径引用

通过后台代码向当前程序的资源中动态添加,代码如下:

1     ResourceDictionary resourceDictionary = new ResourceDictionary(); 2     Application.LoadComponent(resourceDictionary, new Uri("/Test.Resource;component/TextBlockStyle.xaml", UriKind.Relative)); 3     Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);

Application.LoadComponent,只支持相对路径。

通过绝对路径引用

通过绝对路径,可以获取ResourceDictionary下指定Key值的资源。

1     ResourceDictionary normalVersionDict = new ResourceDictionary(); 2     normalVersionDict.Source = new Uri("F:\Github-Myself\KeyBoardEventDemo\WpfApp30\TextBlockStyle.xaml"); 3     var normalVersionDictKeys = normalVersionDict.Keys;

 

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