[英]How to use a dynamic ResourceDictionary Source in Silverlight?
如何在Silverlight中使用動態ResourceDictionary源? 我的應用程序有一個帶有很多樣式定義的“ Styles.xaml”,並引用了定義了幾個畫筆的“ Colors.xaml”:
Styles.xaml:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Project;component/Colors.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="MyLayoutTemplate">
<Button Background="ButtonBackgroundBrush">Button Title</Button>
</DataTemplate>
<!-- A lot of other definitions -->
</ResourceDictionary>
Colors.xaml:
<ResourceDictionary>
<SolidColorBrush x:Key="ButtonBackgroundBrush" Color="#FFFFFFFF"/>
<!-- ... -->
</ResourceDictionary>
因此,基本上,Styles.xaml定義了布局,而Colors.xaml定義了顏色(duh)。 我的App.xaml僅引用此Styles.xaml。
我需要的是一種不使用此方法:
<ResourceDictionary Source="/Project;component/Colors.xaml" />
然后將此Source屬性“指向”(或綁定)到靜態類,在該類中將動態定義它。 像這樣:
<ResourceDictionary Source="{Binding Settings.ThemeUri}" />
有什么辦法可以做到這一點?
您確實只能在XAML中實現此目的,但是用代碼創建字典很簡單。
ResourceDictionary styleDictionary = new ResourceDictionary()
{
Source = new Uri("/Project;component/Styles.xaml", UriKind.Absolute)
};
ResourceDictionary colorDictionary = new ResourceDictionary()
{
Source = new Uri("/Project;component/Colors.xaml", UriKind.Absolute)
};
styleDictionary.MergedDictionaries.Add(colorDictionary);
Application.Current.Resources.MergedDictionaries.Add(styleDictionary);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.