2014-08-31 73 views
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> 

回答

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()); } 
} 

祝您好運!

+0

導航應重新加載的東西?這不適合我。我正在做一個「主頁」,也許這是造成問題的原因。我在關於如何進行母版頁的問題中提供了更多細節。 – 2014-09-06 16:21:25

+0

我刪除了母版頁,但仍無法使用。我必須關閉應用程序並重新啓動它,以便更改背景圖像。 – 2014-09-06 16:31:51