C# WPF在C中设置背景色#

C# WPF在C中设置背景色#,c#,wpf,C#,Wpf,从昨天开始,我在谷歌上搜索了很多,但没有发现多少有意义的东西。 我有一个文件,其中包含一个应用程序的设置,我认为正在正确读取,但更改背景颜色的代码可能不正确 我正在尝试根据设置文件中的信息更改用于窗口背景的颜色 窗口代码 <Window x:Class="WindowFrame.MainWindow" Name="WindowFrame" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xm

从昨天开始,我在谷歌上搜索了很多,但没有发现多少有意义的东西。 我有一个文件,其中包含一个应用程序的设置,我认为正在正确读取,但更改背景颜色的代码可能不正确

我正在尝试根据设置文件中的信息更改用于窗口背景的颜色

窗口代码

<Window x:Class="WindowFrame.MainWindow" Name="WindowFrame"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WindowFrame" 
    ResizeMode="NoResize" 
    ShowInTaskbar="False" 
    Topmost="True" 
    Focusable="False" 
    IsHitTestVisible="False" 
    AllowsTransparency="True" 
    WindowStyle="None" 
    Width="{DynamicResource {x:Static SystemParameters.VirtualScreenWidthKey}}" 
    Height="{DynamicResource {x:Static SystemParameters.VirtualScreenHeightKey}}" 
    HorizontalAlignment="Center" 
    VerticalAlignment="Center" 
    Background="#7FAC0000" 
    WindowState="Maximized">
<Grid>

</Grid>

尝试设置网格的bkg颜色,如
MyGrid.Background=brush,并尝试从
标记中删除额外的属性。您是否单步执行并确保您的代码到达所有这些if语句中?另外,由于您只使用WPF,所以从技术上讲不需要
WindowFrame
<代码>这个
就足够了,甚至只需
Background=brush因为您在那里的窗口的代码后面。不要使用设置文本文件,请使用真实的设置文件,如果需要,您可以使用转换器直接绑定到xaml中的值。但是做主题的正确方法是实际使用主题…。@K.Briggs:看来你修复了昨天的错误,为什么不去昨天的问题,并将其标记为已回答,以帮助这个社区,thx。
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.Runtime.InteropServices;
using System.Windows.Interop;
using System.IO;
using System.Diagnostics;

namespace Dusk_for_Windows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
        public partial class MainWindow : Window
    {
        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hwnd, int index);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

        public MainWindow()
        {
            InitializeComponent();
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.txt");
            string fileContent = File.ReadAllText(filePath);
            string[] contentArray = fileContent.Split(':');
            string contentColor;
            string contentIntensity;

            for (int i = 0; i < contentArray.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        {
                            if (contentArray[i].ToLower().Contains("color: "))
                            {
                                contentColor = contentArray[i].Replace("title: ", "");

                                if(contentColor == "light")
                                {
                                    BrushConverter bc = new BrushConverter();
                                    Brush brush = (Brush)bc.ConvertFrom("#C7DFFC");
                                    brush.Freeze();
                                    WindowFrame.Background = brush;
                                }
                            }

                            if (contentArray[i].ToLower().Contains("intensity: "))
                            {
                                contentIntensity = contentArray[i].Replace("intensity: ", "");
                            }
                            break;
                        }
                }
            }
        }

        public static class WindowsServices
        {
            const int WS_EX_TRANSPARENT = 0x00000020;
            const int GWL_EXSTYLE = (-20);

            public static void SetWindowExTransparent(IntPtr hwnd)
            {
                var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
                SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
            }
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            var hwnd = new WindowInteropHelper(this).Handle;
            WindowsServices.SetWindowExTransparent(hwnd);
        }
    }
}
color: light
intensity: 1