[英]Change Background Image when Theme has been changed
當用戶在其設置中更改手機主題時,我正在嘗試更改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
}
但是我不知道我將如何訪問該屬性...在應用程序仍在運行並且用戶更改主題時,是否可能存在另一種解決方案來更改背景?
您可以嘗試將邏輯放在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;
}
嘗試這個:
型號類別:
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);
}
注意 :
如鏡像所示, 在調試時需要在停用時標記墓碑 。 完成后,它會像您想的那樣工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.