2014-09-02 38 views
0

當用戶在其設置中更改手機主題時,我試圖更改ImageBrush的ImageSource。主題變更時更改背景圖片

所以目前我有這樣的:

XAML:

<ImageBrush ImageSource="{Binding ImageSource, Mode=OneTime}" Stretch="UniformToFill"/> 

代碼隱藏:

public string ImageSource 
{ 
    get 
    { 
     if ((Visibility)App.Current.Resources["PhoneDarkThemeVisibility"] 
      == Visibility.Visible) 
     { 
      return "/Images/BGDark.png"; 

     } 
     else 
     { 
      return "/Images/BG.png"; 

     } 
    } 
    private set { } 
} 

正如你所看到的,我設置背景當應用程序已經啓動。該應用程序將通過查看選擇哪個主題來設置背景。

我現在的問題是,當用戶轉到設置(應用程序尚未關閉!)並更改主題時,Background/ImageSource將不會更新,當他激活應用程序時。 我想,也許我可以在這裏設置新的背景變化吧:

App.xaml.cs:

private void Application_Activated(object sender, ActivatedEventArgs e) 
{ 
    //access somehow the CodeBehind of my MainPage and change the ImageSource 
} 

但我不知道我怎麼會訪問屬性... 當應用程序仍在運行並且用戶更改主題時,是否有另一種更改背景的解決方案?

回答

1

試試這個:

模型類:

class SampleClass 
{ 
    public string ImageSource 
    { 
     get 
     { 
      if ((Visibility)App.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible) 
      { 
       return "Dark";//You can set your Image here 

      } 
      else 
      { 
       return "Light";//You can set your Image here 

      } 
     } 
     private set { } 
    } 
} 

用法:

private void Application_Activated(object sender, ActivatedEventArgs e) 
{ 
    SampleClass obj = new SampleClass(); 
    Debug.WriteLine(obj.ImageSource); 
} 

注:

需要您標記一旦去激活墓碑而如在圖像調試。這樣做後,它會工作,因爲你應該這樣做tombstone upon deactivation while debugging

+0

工作正常,謝謝:) – Rudi 2014-09-02 12:22:32

1

你可以嘗試把邏輯Page.OnNavigatedTo()而不是Application_Activated()

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    //do some logic to check if changing Background is necessary 
    //if it is then change the Background, else simply return; 
} 
+0

也試過這個,但App.Current.Resources未被「更新」。當我以黑色主題啓動應用程序時,資源將爲黑色。如果我改變它,資源仍然是黑色的,而不是白色.. – Rudi 2014-09-02 12:23:41