我已经在我的Windows Phone应用程序中创建了几个资源词典,并在App.xaml.cs文件中添加了以下代码来加载主题:
private void LoadDictionary()
{
var dictionaries = Resources.MergedDictionaries;
dictionaries.Clear();
string source = String.Format("/MyApp;component/Themes/Theme2.xaml");
var themeStyles = new ResourceDictionary { Source = new Uri(source, UriKind.Relative) };
dictionaries.Add(themeStyles);
}
当我更改代码并重新编译它时,这工作正常,但我想要并且无法弄清楚的是; 当用户从设置页面选择主题时,如何加载字典?
我在 XAML 中将样式设置为文本框的方式如下所示:
<TextBox x:Name="Count" Grid.Column="1" TextAlignment="Center" InputScope="Number">
<TextBox.Style>
<StaticResource ResourceKey="InputTextBox" />
</TextBox.Style>
</TextBox>
您通常使用 DynamicResource 执行此操作(请参阅本文 WPF Switch 颜色主题),但不幸的是,我们在 Windows Phone 中没有这种奢侈。
我唯一的建议是使用具有要交换的内容的属性的 Global 类,然后通过 SetTheme() 方法手动处理交换。