主题更改后更改背景图像

时间:2014-09-02 11:08:46

标签: c# xaml binding windows-phone

当用户在其设置中更改手机主题时,我尝试更改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 { }
}

如您所见,我在应用程序启动时设置了背景。该应用程序将通过查看已选择的主题来设置背景。

我现在的问题是,当用户进入设置(应用程序尚未关闭!)并更改主题时,后台/图像源在激活应用程序交易时不会更新。 我以为我可以通过在这里设置新背景来改变它:

App.xaml.cs:

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

但我不知道如何进入该物业...... 是否有其他解决方案可以在应用程序仍在运行且用户更改主题时更改背景?

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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

相关问题