立即应用全局主题更改

时间:2014-08-31 17:39:51

标签: windows-runtime winrt-xaml

我的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>
  • 主题更改会应用于某些内容,例如StyleNavigationBackButtonNormalStyle的更改。
  • 我尝试进行Style更改,而不是Background,但也无效。

更新

我也有一种“Master Page”设置。这就是它的创造方式。

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>

1 个答案:

答案 0 :(得分:1)

只需重新加载页面。

你可以试试这个:

public bool Reload(object param = null)
{
    Type type = this.Frame.CurrentSourcePageType;
    if (this.Frame.BackStack.Any())
    {
        type = this.Frame.BackStack.Last().SourcePageType;
        param = this.Frame.BackStack.Last().Parameter;
    }
    try { return this.Frame.Navigate(type, param); }
    finally { this.Frame.BackStack.Remove(this.Frame.BackStack.Last()); }
}

祝你好运!

相关问题