C# 我的资源字典没有显示我的标签

C# 我的资源字典没有显示我的标签,c#,wpf,resourcedictionary,C#,Wpf,Resourcedictionary,我有一个wpf应用程序,正在加载主题(浅色和深色),我制作了两个简单的资源字典文件,它们是在共享程序集中创建的: 深色主题(浅色主题的结构相同,但颜色值不同): 在我的主应用程序App.xaml中,我引用了两个主题词典 <Application x:Class="Foo.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="htt

我有一个wpf应用程序,正在加载主题(浅色和深色),我制作了两个简单的资源字典文件,它们是在共享程序集中创建的:

深色主题(浅色主题的结构相同,但颜色值不同):


在我的主应用程序App.xaml中,我引用了两个主题词典

<Application x:Class="Foo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Foo.Core.WPF;component/Resources/Dictionary_DarkTheme.xaml" x:Name="DarkTheme"/>
                <ResourceDictionary Source="pack://application:,,,/Foo.Core.WPF;component/Resources/Dictionary_LightTheme.xaml" x:Name="LightTheme"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我根据选择的主题设置资源的方式在App.xaml.cs中完成

public enum Skin { Light, Dark }

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static Skin Skin { get; set; }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            ChangeSkin(Skin.Light);
        }

        public void ChangeSkin(Skin newSkin)
        {
            Skin = newSkin;

            if (Skin == Skin.Dark)
                ApplyResources(Resources.MergedDictionaries[0].Source.ToString());
            else if (Skin == Skin.Light)
                ApplyResources(Resources.MergedDictionaries[1].Source.ToString());
        }

        private void ApplyResources(string src)
        {
            var dict = new ResourceDictionary() { Source = new Uri(src, UriKind.RelativeOrAbsolute) };
            foreach (var mergeDict in dict.MergedDictionaries)
            {
                Resources.MergedDictionaries.Add(mergeDict);
            }

            foreach (var key in dict.Keys)
            {
                Resources[key] = dict[key];
            }
        }
    }
public枚举皮肤{浅,暗}
/// 
///App.xaml的交互逻辑
/// 
公共部分类应用程序:应用程序
{
公共静态皮肤{get;set;}
启动时受保护的覆盖无效(StartupEventArgs e)
{
基础。启动时(e);
改变皮肤(皮肤。光);
}
公共皮肤(皮肤新闻皮肤)
{
皮肤=新闻纸;
如果(皮肤==皮肤暗)
ApplyResources(Resources.MergedDictionaries[0].Source.ToString());
else if(皮肤==皮肤光)
ApplyResources(Resources.MergedDictionaries[1].Source.ToString());
}
私有void ApplyResources(字符串src)
{
var dict=new ResourceDictionary(){Source=new Uri(src,UriKind.RelativeOrAbsolute)};
foreach(dict.MergedDictionaries中的var mergeDict)
{
Resources.MergedDictionaries.Add(mergeDict);
}
foreach(dict.Keys中的var键)
{
资源[key]=dict[key];
}
}
}
最后是我的主窗口。因为我希望这些特定的样式是全局的,所以我不使用任何键来识别它们

<Window x:Class="Foo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Label Content="hello"/>
    </Grid>
</Window>


但我的主要问题是Label控件没有显示在我的应用程序中。我可以看到我的背景颜色适当改变,但我的标签控件刚刚消失!我做错了什么?非常感谢

不要将所有主题资源字典从开始添加到
应用程序.Resources.MergedDictionaries
,即从空的
应用程序.Resources
开始:

<Application x:Class="Foo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
    </Application.Resources>
</Application>

当更改主题仅意味着替换颜色和画笔时,您还可以将样式移动到
Application.Resources
,并在样式设置器中使用
DynamicResource

<Application x:Class="Foo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="{DynamicResource TextColorBrush}"/>
        </Style>
        <Style TargetType="Grid">
            <Setter Property="Background" Value="{DynamicResource Background}"/>
        </Style>
    </Application.Resources>
</Application>

那么您的主题资源字典将只包含颜色和笔刷资源:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush Color="#FF1E1E1E" x:Key="Background"/>
    <SolidColorBrush x:Key="TextColorBrush" Color="White"/>
</ResourceDictionary>

不要将所有主题资源字典从开始添加到
应用程序.Resources.MergedDictionaries
,即从空的
应用程序.Resources开始:

<Application x:Class="Foo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
    </Application.Resources>
</Application>

当更改主题仅意味着替换颜色和画笔时,您还可以将样式移动到
Application.Resources
,并在样式设置器中使用
DynamicResource

<Application x:Class="Foo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="{DynamicResource TextColorBrush}"/>
        </Style>
        <Style TargetType="Grid">
            <Setter Property="Background" Value="{DynamicResource Background}"/>
        </Style>
    </Application.Resources>
</Application>

那么您的主题资源字典将只包含颜色和笔刷资源:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush Color="#FF1E1E1E" x:Key="Background"/>
    <SolidColorBrush x:Key="TextColorBrush" Color="White"/>
</ResourceDictionary>


再次感谢您,我明白您的意思。我想做的是使解决方案中的其他项目可以访问这些词典。这样,任何新的WPF项目都会引用这些字典所在的共享项目,然后它们就可以得到相同的“皮肤”。再次感谢您,我明白您的观点。我想做的是使解决方案中的其他项目可以访问这些词典。这样,任何新的WPF项目都会引用这些字典所在的共享项目,然后它们就可以得到相同的“皮肤”。