1
我的WinRT应用程序用户可以将其数据与服务器同步。一些同步的数据是应用程序的全局主题更改。立即应用全局主题更改
我去通过动态创建一个XAML
文件,然后做这个不断变化的全球主题。
var resource = (ResourceDictionary)XamlReader.Load(content);
然后我通过这样覆盖应用程序的全局主题。
var resources = new ResourceDictionary();
var converters = new ResourceDictionary { Source = new Uri("ms-appx:/Resources/Converters.xaml") };
var callisto = new ResourceDictionary { Source = new Uri("ms-appx:/Resources/Callisto.xaml") };
var templates = new ResourceDictionary { Source = new Uri("ms-appx:/Resources/Templates.xaml") };
resources.MergedDictionaries.Add(converters);
resources.MergedDictionaries.Add(callisto);
resources.MergedDictionaries.Add(templates);
resources.MergedDictionaries.Add(resource);
App.Current.Resources = resources;
资源文件包含此项。
<ImageBrush x:Key="ApplicationPageBackgroundThemeImageBrush" ImageSource="%%ThemeBackground%%" Stretch="UniformToFill" />
将%%ThemeBackground%%
替换为实际的文件位置。
其中一些更改立即生效,如NavigationBackButtonNormalStyle
样式,但其他更改不会,例如ImageBrush
。这些更改仅在应用程序再次启动时显示,并且此代码在App.xaml.cs
的应用程序启动期间运行,而不是从正在运行的页面运行。
它这甚至可能吗?
上是如何工作的一些注意事项。
- 设置主题的代码位于单独的类中,可以在应用程序的任何位置调用。
- 的
ImageBrush
被施加到该<Border Background="{ThemeResource ApplicationPageBackgroundThemeImageBrush}"></Border>
。 - 主题的变化确实会应用到一些东西像
Style
变化NavigationBackButtonNormalStyle
。 - 我试着做一个
Style
变化,而不是Background
,并且也不能工作。
更新
我也有几分这个 “母版页” 设置的。这是如何创建的。
var currentFrame = (Frame)Window.Current.Content;
var masterPage = new MasterPage
{
ContentFrame = currentFrame,
};
Window.Current.Content = masterPage;
主页面只包含TopAppBar
。
<Page.TopAppBar>
<!-- buttons here -->
</Page.TopAppBar>
<Grid>
<ContentControl Content="{Binding ContentFrame, ElementName=PageRoot}" />
</Grid>
未更新的背景图像在每个页面上都是这样应用的。
<Border Background="{ThemeResource ApplicationPageBackgroundThemeImageBrush}"></Border>
导航应重新加载的东西?这不适合我。我正在做一个“主页”,也许这是造成问题的原因。我在关于如何进行母版页的问题中提供了更多细节。 – 2014-09-06 16:21:25
我删除了母版页,但仍无法使用。我必须关闭应用程序并重新启动它,以便更改背景图像。 – 2014-09-06 16:31:51