0

我在ResourceDictionary. 我创建了一个窗口来更改 ThemeColor。这种颜色绑定到我的ResourceDictionary.

这是我的资源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ns="clr-namespace:Phoenix_CRM" >


    <LinearGradientBrush x:Key="DegradeCouleurTheme" StartPoint="0,0" EndPoint="0,1" >
        <GradientStop Offset="0" Color="{x:Static ns:CParametres.ColorBegin}" />
        <GradientStop Offset="1" Color="{x:Static ns:CParametres.ColorEnd}"/>
    </LinearGradientBrush>
</ResourceDictionary>

我创建了一个 CParameters 类来设置颜色参数。构成渐变的两种颜色保存在数据库中。此类旨在加载/保存颜色并应用它。

这是我更改颜色的课程:

    public class CParametres : INotifyPropertyChanged
    {

        private  Color m_ThemeColorGradientBegin;
        public  Color ThemeColorGradientBegin
        {
            get { return m_ThemeColorGradientBegin; }
            set
            {
                m_ThemeColorGradientBegin = value;
                ColorBegin = m_ThemeColorGradientBegin;
                FirePropertyChangedEvent("ColorBegin");
            }
        }

        private  Color m_ThemeColorGradientEnd;
        public  Color ThemeColorGradientEnd
        {
            get { return m_ThemeColorGradientEnd; }
            set
            {
                m_ThemeColorGradientEnd = value;
                ColorEnd = m_ThemeColorGradientEnd;
                FirePropertyChangedEvent("ColorEnd");
            }
        }

        public static Color ColorBegin;
        public static Color ColorEnd;


        public CParametres()
        {
            ....
            ....
        }

        public void LoadGradientDefault()
        {
            ThemeColorGradientBegin   =   (Color)ColorConverter.ConvertFromString("#00b6e7");
            ThemeColorGradientEnd     =   (Color)ColorConverter.ConvertFromString("#0086d6");
        }

        public void LoadParams()
        {
            if (ReadParamFromDB() == true)
            {
                setThemeGradient(m_ThemeColorGradientBegin, m_ThemeColorGradientEnd);
            }
        }

        public void setThemeGradient(Color ColorBegin, Color ColorEnd)
        {
            this.ThemeColorGradientBegin = ColorBegin;
            this.ThemeColorGradientEnd = ColorEnd;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void FirePropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

当我更改颜色时,我需要退出并重新运行我的应用程序才能看到更改。请问有人知道强制直接应用新颜色吗?

任何人都可以向我解释一下刷新 UI 的提示是什么?

4

2 回答 2

1

您需要对所有资源颜色和样式使用 DynamicResource 而不是 StaticResource。

使用 DynamicResource,您的所有更改样式都将反映在 UI 中。

更新:

当你使用Brush时,你需要以这种方式给Brush

<Setter Property="Foreground">
    <Setter.Value>
        <SolidColorBrush Color="{DynamicResource AccentColor}" />
    </Setter.Value>
</Setter>
于 2013-04-11T15:11:22.150 回答
-1

您需要实现 INotifyPropertyChanged 接口才能发生属性更改事件。不幸的是,这意味着您需要一个实例来操作而不是静态的。

如果您创建一个返回单例实例的静态属性,您可以执行以下操作

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ns="clr-namespace:Phoenix_CRM" >


    <LinearGradientColor x:Key="DegradeCouleurTheme" StartPoint="0,0" EndPoint="0,1" >
        <GradientStop Offset="0" Color="{Binding ColorBegin, Source={x:Static ns:CParametres.Instance}}" />
        <GradientStop Offset="1" Color="{Binding ColorEnd, Source={x:Static ns:CParametres.Instance}}" />
    </LinearGradientColor>
</ResourceDictionary>

单例示例

public class CParameters : INotifyPropertyChanged {

  public event PropertyChangedEventHandler PropertyChanged;

  private void OnPropertyChanged(string propertyName) {
    var handle = PropertyChanged;
    if (handle != null) handle(this, new PropertyChangedEventArgs(propertyName));
  }

  private static readonly CParameters instance = new CParameters();

  public static CParameters Instance {
     get { return instance; }
  }

  private Color colorBegin;

  private Color colorEnd;

  public Color ColorBegin {
    get { return colorBegin; }
    set {
      if (value != colorBegin) {
        colorBegin=value;
        OnPropertyChanged("ColorBegin");
      }
    }
  }


  public Color ColorEnd {
    get { return colorEnd; }
    set { 
     if (value != colorEnd) {
       colorEnd = value;
       OnPropertyChanged("ColorEnd");
     }
   }
  }
}
于 2013-04-11T15:13:00.860 回答