首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF项目DarkMode和LightMode问题

WPF项目DarkMode和LightMode问题
EN

Stack Overflow用户
提问于 2020-08-23 18:56:48
回答 2查看 208关注 0票数 0

我有Wpf C#.net项目。

我想做清单项目,但我有问题。

在我的项目中,我使用寄存器键来命名主题,然后在我的项目中检索它们并使用它们,但是我有一个严重的问题,我的问题是当我点击按钮时,按钮主题会改变,但是主窗口不会改变。

请帮我解决这个问题。这是我用来创建、设置和检索注册表项的代码的宁静:

代码语言:javascript
复制
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace HiPlanner.Classes
{
    public class General_Info_Class01
    {
        public double SysMonSizeWidth = SystemParameters.PrimaryScreenWidth;
        public double SysMonSizeHeight = SystemParameters.PrimaryScreenHeight;

        public byte ThemeRegisteryNum()
        {
            RegistryKey ThemeStatusRegKey = Registry.CurrentUser.OpenSubKey(@"SoftWare/HiPlanner");
            Byte Result = byte.Parse(ThemeStatusRegKey.GetValue("ThemeNumber").ToString());
            ThemeStatusRegKey.Close();
            return Result;
        }

        public void SetThemeRegNumBtnMethod(byte ThemeNo)
        {
            RegistryKey ThemeRegKey = Registry.CurrentUser.OpenSubKey(@"SoftWare/HiPlanner",true);
            ThemeRegKey.SetValue("ThemeNumber", ThemeNo);
            ThemeRegKey.Close();
        }

    }
}

这是我创建的用户控件,它是一个暗而轻的主题按钮:

设计规范:

代码语言:javascript
复制
<UserControl x:Class="HiPlanner.DesignFiles.UserControls.ThemeStatusButton"
             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:local="clr-namespace:HiPlanner.DesignFiles.UserControls"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid x:Name="TSBTNMainGrid" Style="{StaticResource TSBTNGridStyle}">
        <Button x:Name="ThemeStatusBTN" Style="{StaticResource StatusBTN-WhiteMode}" Click="ThemeStatusBTN_Click">
            <Border x:Name="StatusCircleBorder" Style="{StaticResource StatusBorder-WhiteMode}">
                <Border.RenderTransform>
                    <TranslateTransform/>
                </Border.RenderTransform>
                <Image x:Name="ThemeStatusBtnImg" Style="{StaticResource StatusBtnImgStyle-WhiteMode}"/>
            </Border>
        </Button>
    </Grid>
</UserControl>

design dictionary :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:HiPlanner.DesignFiles.DesignDictionaries">

    <!--Start Status Button Style-->


    <Style TargetType="Grid" x:Key="TSBTNGridStyle">
        <Setter Property="Width" Value="110"/>
        <Setter Property="Height" Value="60"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality"/>
    </Style>
    
    <Style TargetType="Button" x:Key="StatusBTN-WhiteMode">
        <Setter Property="Width" Value="100"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="Background" Value="#E6E6E6"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                    <GradientStop Color="#005DDE" Offset="0"/>
                    <GradientStop Color="#00A8DE" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="Button" x:Key="StatusBTN-DarkMode">
        <Setter Property="Width" Value="100"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="Background" Value="#1A1D24"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                    <GradientStop Color="#005DDE" Offset="0"/>
                    <GradientStop Color="#00A8DE" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <Style TargetType="Border" x:Key="StatusBorder-WhiteMode">
        <Setter Property="Width" Value="30"/>
        <Setter Property="Height" Value="30"/>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                    <GradientStop Color="#005DDE" Offset="0"/>
                    <GradientStop Color="#00A8DE" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="Background" Value="#E3E3E3"/>
        <Setter Property="CornerRadius" Value="30"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Effect">
            <Setter.Value>
                <DropShadowEffect Color="#D1D1D1" BlurRadius="10" Direction="360" Opacity="1" RenderingBias="Quality" RenderOptions.BitmapScalingMode="HighQuality" ShadowDepth="0"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Margin" Value="50,0,0,0"/>
    </Style>

    <Style TargetType="Border" x:Key="StatusBorder-DarkMode">
        <Setter Property="Width" Value="30"/>
        <Setter Property="Height" Value="30"/>
        <Setter Property="BorderBrush" Value="#002136"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="Background" Value="#002A45"/>
        <Setter Property="CornerRadius" Value="30"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Effect">
            <Setter.Value>
                <DropShadowEffect Color="#002136" BlurRadius="10" Direction="360" Opacity="1" RenderingBias="Quality" RenderOptions.BitmapScalingMode="HighQuality" ShadowDepth="0"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Margin" Value="0,0,50,0"/>
    </Style>

    <Style TargetType="Image" x:Key="StatusBtnImgStyle-WhiteMode">
        <Setter Property="Width" Value="Auto"/>
        <Setter Property="Height" Value="Auto"/>
        <Setter Property="Stretch" Value="UniformToFill"/>
        <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality"/>
        <Setter Property="Source" Value="/HiPlanner;component/DesignFiles/Photos/StatusBtn-WhiteMode.png"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="Margin" Value="1"/>
    </Style>

    <Style TargetType="Image" x:Key="StatusBtnImgStyle-DarkMode">
        <Setter Property="Width" Value="Auto"/>
        <Setter Property="Height" Value="Auto"/>
        <Setter Property="Stretch" Value="UniformToFill"/>
        <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality"/>
        <Setter Property="Source" Value="/HiPlanner;component/DesignFiles/Photos/StatusBtn-DarkMode.png"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
    </Style>
    <!--END Status Button Style-->
</ResourceDictionary>

按钮后面的c#代码:

代码语言:javascript
复制
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HiPlanner.DesignFiles.UserControls
{
    /// <summary>
    /// Interaction logic for ThemeStatusButton.xaml
    /// </summary>
    public partial class ThemeStatusButton : UserControl
    {
        Classes.General_Info_Class01 GeneralInfo = new Classes.General_Info_Class01();
        Style ThemeStatusBtnStyleWhiteMode = (Style)Application.Current.FindResource("StatusBTN-WhiteMode");
        Style ThemeStatusBtnStyleDarkMode = (Style)Application.Current.FindResource("StatusBTN-DarkMode");
        Style ThemeStatusBorderStyleWhiteMode = (Style)Application.Current.FindResource("StatusBorder-WhiteMode");
        Style ThemeStatusBorderStyleDarkMode = (Style)Application.Current.FindResource("StatusBorder-DarkMode");
        Style ThemeStatusImgStyleWhiteMode = (Style)Application.Current.FindResource("StatusBtnImgStyle-WhiteMode");
        Style ThemeStatusImgStyleDarkMode = (Style)Application.Current.FindResource("StatusBtnImgStyle-DarkMode");


        public ThemeStatusButton()
        {
            InitializeComponent();
            SetDefaultThemeBTnTheme();
        }

        private void SetDefaultThemeBTnTheme()
        {
            if (GeneralInfo.ThemeRegisteryNum() == 0)
            {
                ThemeStatusBTN.Style = ThemeStatusBtnStyleWhiteMode;
                StatusCircleBorder.Style = ThemeStatusBorderStyleWhiteMode;
                ThemeStatusBtnImg.Style = ThemeStatusImgStyleWhiteMode;
            }
            else if (GeneralInfo.ThemeRegisteryNum() == 1)
            {
                ThemeStatusBTN.Style = ThemeStatusBtnStyleDarkMode;
                StatusCircleBorder.Style = ThemeStatusBorderStyleDarkMode;
                ThemeStatusBtnImg.Style = ThemeStatusImgStyleDarkMode;
            }
            else
            {
                ThemeStatusBTN.Style = ThemeStatusBtnStyleWhiteMode;
                StatusCircleBorder.Style = ThemeStatusBorderStyleWhiteMode;
                ThemeStatusBtnImg.Style = ThemeStatusImgStyleWhiteMode;
            }
        }

        private void ThemeStatusBTN_Click(object sender, RoutedEventArgs e)
        {

            if (GeneralInfo.ThemeRegisteryNum() == 0)
            {
                GeneralInfo.SetThemeRegNumBtnMethod(1);
                ThemeStatusBTN.Style = ThemeStatusBtnStyleDarkMode;
                StatusCircleBorder.Style = ThemeStatusBorderStyleDarkMode;
                ThemeStatusBtnImg.Style = ThemeStatusImgStyleDarkMode;

<-- problem is here -->

                RunMainWindowThemeChecker();

<-- problem is here -->

            }
            else
            {
                GeneralInfo.SetThemeRegNumBtnMethod(0);
                ThemeStatusBTN.Style = ThemeStatusBtnStyleWhiteMode;
                StatusCircleBorder.Style = ThemeStatusBorderStyleWhiteMode;
                ThemeStatusBtnImg.Style = ThemeStatusImgStyleWhiteMode;
                RunMainWindowThemeChecker();
            }
        }
<-- problem is here -->
        private void RunMainWindowThemeChecker()
        {
            MainWindow MyMainWindow = new MainWindow(0);
            MyMainWindow.SetWindowTheme();
        }

<-- problem is here -->

    }

后面的主窗口代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HiPlanner;


namespace HiPlanner
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Classes.General_Info_Class01 GeneralInfo = new Classes.General_Info_Class01();

        public MainWindow()
        {
            InitializeComponent();
            UserResizeWindow();
            SetWindowTheme();
        }

        public MainWindow(byte none)
        {
            InitializeComponent();
        }


        private void UserResizeWindow()
        {

            double UserSysMonSizeWidth = GeneralInfo.SysMonSizeWidth;
            double UserSysMonSizeHeight = GeneralInfo.SysMonSizeHeight;
            Style FullHDStyle = (Style)Application.Current.FindResource("FullHDWindowStyle");
            Style HDStyle = (Style)Application.Current.FindResource("HDWindowStyle");

            if (UserSysMonSizeWidth >= 1920 && UserSysMonSizeHeight >= 1080)
            {
                MainWindow01.Style = FullHDStyle;
            }
            else
            {
                MainWindow01.Style = HDStyle;
            }

        }
<-- problem is here -->
        public void SetWindowTheme()
        {

            Classes.General_Info_Class01 SecGeneralinfo = new Classes.General_Info_Class01();
            Style WhiteModeTheme = (Style)Application.Current.FindResource("MainBorderStyle-WhiteMode");
            Style DarkModeTheme = (Style)Application.Current.FindResource("MainBorderStyle-DarkMode");
            Style WhiteModeLogoIcon = (Style)Application.Current.FindResource("LogoBorderStyle-WhiteMode");
            Style DarkModeLogoIcon = (Style)Application.Current.FindResource("LogoBorderStyle-DarkMode");
            Style WhiteModeLogoText = (Style)Application.Current.FindResource("LogoTextLBL");
            Style DarkModeLogoText = (Style)Application.Current.FindResource("LogoTextLBL-DarkMode");

            if (SecGeneralinfo.ThemeRegisteryNum() == 0)
            {
                MainWindowBorder.Style = WhiteModeTheme;
                LogoBorder.Style = WhiteModeLogoIcon;
                LogoTextLabel.Style = WhiteModeLogoText;
            }
            else if (SecGeneralinfo.ThemeRegisteryNum() == 1)
            {
                MainWindowBorder.Style = DarkModeTheme;
                LogoBorder.Style = DarkModeLogoIcon;
                LogoTextLabel.Style = DarkModeLogoText;
            }
            else
            {
                MainWindowBorder.Style = WhiteModeTheme;
                LogoBorder.Style = WhiteModeLogoIcon;
                LogoTextLabel.Style = WhiteModeLogoText;
            }

        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Testbtn_Click(object sender, RoutedEventArgs e)
        {
            Windows.AddPlanWin Addplan = new Windows.AddPlanWin();
            Addplan.ShowDialog();
        }

<-- problem is here -->
    }
}

我知道很长,但请帮帮我。

事实上,我想知道怎样才能像按钮一样实时改变主窗口的主题?

向任何人致以最良好的问候。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-24 01:29:58

我所做的是使用动态资源而不是静态资源,您可能需要更多地了解它。在我的应用程序中,我有一个与您非常相似的资源字典。然而,我有两个不同的“主题”配色方案ADA 508颜色遵从的目的。一种颜色方案允许渐变主题,另一种是更平坦/固定的颜色。

在各自的"ResourceDictionaries“中,我用相同的名称定义了我的颜色。你所做的是一个明确的白色和黑暗的背景。

当我单击按钮在渐变和ADA兼容之间切换时,我只加载相应的资源字典,当表单被创建/刷新时,那些新加载的颜色值就会获胜。

就像这个片段..。其实我还有更多但只是展示..。默认情况下,我在应用程序中加载了默认的"MyColors.xaml“。

代码语言:javascript
复制
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary  Source="pack://application:,,,/MyApp;component/Themes/MyConverters.xaml" />
            <ResourceDictionary  Source="pack://application:,,,/MyApp;component/Themes/MyImages.xaml" />
            <ResourceDictionary  Source="pack://application:,,,/MyApp;component/Themes/MyColors.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

然后,在MyColors.xaml里,我可能

代码语言:javascript
复制
<SolidColorBrush po:Freeze="True" x:Key="BrushWindowTitleBar" Color="#FFA0A0A0" />

<Color x:Key="MainWindowGradient1">#FF303030</Color>
<Color x:Key="MainWindowGradient2">#FF404040</Color>
<Color x:Key="MainWindowGradient3">#FF505050</Color>

<LinearGradientBrush x:Key="LGBMainWindowBackground" StartPoint="0,0" EndPoint="0,1" >
    <GradientStop Color="{DynamicResource MainWindowGradient1}" Offset="0" />
    <GradientStop Color="{DynamicResource MainWindowGradient2}" Offset="0.45" />
    <GradientStop Color="{DynamicResource MainWindowGradient3}" Offset="1" />
</LinearGradientBrush>

等等。

然后,在我的MyCols508.xaml中,我会使用相同的颜色名称,但是有不同的值

代码语言:javascript
复制
<SolidColorBrush po:Freeze="True" x:Key="BrushWindowTitleBar" Color="#FF545454" />

<Color x:Key="MainWindowGradient1">#FF303030</Color>
<Color x:Key="MainWindowGradient2">#FF333333</Color>
<Color x:Key="MainWindowGradient3">#FF383838</Color>

<LinearGradientBrush x:Key="LGBMainWindowBackground" StartPoint="0,0" EndPoint="0,1" >
    <GradientStop Color="{DynamicResource MainWindowGradient1}" Offset="0" />
    <GradientStop Color="{DynamicResource MainWindowGradient2}" Offset="0.45" />
    <GradientStop Color="{DynamicResource MainWindowGradient3}" Offset="1" />
</LinearGradientBrush>

然后,在我的代码中有一个方法来交换.基于"MyColors.xaml“和”MyCols508.xaml“资源中文件的命名约定,我只是加载备用的.

代码语言:javascript
复制
    public static void ChangeColorScheme(bool show508ColorCompliance)
    {
        _show508ColorCompliance = show508ColorCompliance;

        // do we or not show the 508 color compliance theme of colors
        var PackColorScheme = @"pack://application:,,,/MyApp;component/Themes/MyColors{0}.xaml";
        var _uriPath = new Uri(
            string.Format(PackColorScheme, _show508ColorCompliance ? "508" : ""), UriKind.RelativeOrAbsolute);
        var dict = new ResourceDictionary();
        try
        {
            dict.Source = _uriPath;
            var app = Application.Current;
            app.Resources.MergedDictionaries.Add(dict);
        }
        catch (Exception e1)
        {
            MessageBox.Show(e1.Message);
        }

    }
票数 2
EN

Stack Overflow用户

发布于 2021-11-02 14:56:31

在创建UserControl时,我第一次体验了黑暗模式,这就是我如何使用Visual文档这里并回答它的方法。

默认情况下,用户控件是透明的。票上已经提供了一个解决方案。或者,如果要更改用户控件的背景色,可以在用户控件上添加以d16.8开头的d:Background=“White”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63550903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档