在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;
?