【问题标题】:Change Background Image when Theme has been changed更改主题时更改背景图像
【发布时间】:2014-09-02 11:08:45
【问题描述】:

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

但我不知道如何访问该属性... 是否有其他解决方案可以在应用仍在运行且用户更改主题时更改背景?

【问题讨论】:

    标签: c# xaml binding windows-phone


    【解决方案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);
    }
    

    注意:

    您需要在调试时标记停用时的墓碑,如图所示。完成后,它会像你想象的那样工作

    【讨论】:

      【解决方案2】:

      您可以尝试将逻辑放入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;
      }
      

      【讨论】:

      • 也试过这个,但 App.Current.Resources 还没有“更新”。当我以黑色主题启动应用程序时,资源将是黑色的。如果我改变它,资源仍然是黑色而不是白色..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      相关资源
      最近更新 更多