在论坛里面搜了很多相似的问题,但是都不能在代码中动态地更改主题。
App.Xaml中加入自定义的两个主题文件:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
<ResourceDictionary Source="Themes/CuteTheme.xaml"/>
<ResourceDictionary Source="Themes/ProfessionalTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
我有一个App Settings,其中一个Combox是更改主题,当该Combox改变时,调用App的静态方法,如下:
void App::ChangeTheme(String^ themeName)
{
if(themeName != nullptr && !themeName->IsEmpty())
{
ResourceDictionary^ dic = ref new ResourceDictionary();
String^ themePath = "ms-appx:///Themes/" + themeName + "Theme.xaml";
Uri^ uri = ref new Uri(themePath);
Application::LoadComponent(dic,uri,ComponentResourceLocation::Application);
ResourceDictionary^ standardDic = ref new ResourceDictionary();
//将标准样式库加载进去
Uri^ standardUri = ref new Uri("ms-appx:///Common/StandardStyles.xaml");
Application::LoadComponent(standardDic,standardUri,ComponentResourceLocation::Application);
App::Current->Resources->MergedDictionaries->Clear();
App::Current->Resources->MergedDictionaries->Append(standardDic);
App::Current->Resources->MergedDictionaries->Append(dic);
}
}
但是,即使这样编码,样式还是不能改变,即使关闭应用再打开也不行。网上有种说法是要重新将View创建 一下,如何重新创建view?另外还有其他办法没?
谢谢!
Dino Wu