wpf - 无法动态更改 ui 颜色

问题描述 投票:0回答:1

我希望能够更改工具窗口的背景颜色(它是 VSIX,而不是应用程序)

所以我需要以编程方式让PanelBackground指向PanelBackgroundLight或PanelBackgroundDark

这是我使用的样式表(删除了标题)

<SolidColorBrush x:Key="PanelBackgroundLight" Color="#FFFFFFFF"/>
<SolidColorBrush x:Key="PanelBackgroundDark" Color="#FF000000"/>

<SolidColorBrush x:Key="PanelBackground" Color="{Binding Source={StaticResource PanelBackgroundLight}, Path=Color}"/>

我使用这个函数来触发更新UI,无论我尝试什么,背景颜色都不会改变

public partial class MyControl : UserControl
{
  public void UpdateColors()
  {
     Dispatcher.Invoke(() =>
     {
         ThreadHelper.ThrowIfNotOnUIThread();

         // this changes colors in the resource dictionary
         var themeChanger = new ThemeChanger();
         themeChanger.UpdateColors();

         // this is supposed to force the UI to refresh
         InvalidateVisual();
         Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
         Arrange(new Rect(0, 0, DesiredSize.Width, DesiredSize.Height));
         UpdateLayout();
     });
  }
...

这是我为我的主题创建的类

public class ThemeChanger
{
    private readonly ResourceDictionary _resourceDictionary;

    public ThemeChanger()
    {
        _resourceDictionary = new ResourceDictionary
        {
            Source = new Uri("pack://application:,,,/LSPConector2019;component/wpf/styles/Styles.xaml")
        };
    }

    public void UpdateColors()
    {
        var darkBrush = ((SolidColorBrush)_resourceDictionary["PanelBackgroundDark"]);
        _resourceDictionary["PanelBackground"] = new SolidColorBrush(darkBrush.Color);
    }
}

主要用户控件xaml

<UserControl x:Class="MyNameSpace.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
             xmlns:vsplatformui="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.15.0"
             mc:Ignorable="d"
             Background="{DynamicResource {x:Static vsshell:VsBrushes.ToolWindowBackgroundKey}}"
             Foreground="Black"
             Name="MyToolWindow"
             d:DesignWidth ="1000"
             d:DesignHeight="500">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MyNamespace;component/wpf/styles/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

  <!-- also tried DynamicResource  -->
  <Grid x:Name="mainGrid" Background="{StaticResource PanelBackground}">
  ...

任何帮助表示赞赏

c# wpf colors themes
1个回答
0
投票

这不好

_resourceDictionary = new ResourceDictionary
{
    Source = new Uri("pack://application:,,,/LSPConector2019;component/wpf/styles/Styles.xaml")
};

要使用的词典位于用户控制资源中

m_themeChanger = new ThemeChanger(Resources.MergedDictionaries);

从此,只需按照Mustafa提供的代码修改颜色即可

克莱门斯是对的,不需要花哨的东西

public void UpdateColors(string theme)
{
    Dispatcher.Invoke(() =>
    {
        m_themeChanger.UpdateColors(theme);
    });
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.