首页 > 解决方案 > My resource dictionary is omitting my label from being showed

问题描述

I have a wpf app and I am messing with loading themes (light and dark), I made two simple resource dictionary files which are created in a shared assembly:

Dark Theme (same structure for the light theme, but with different color values):

<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"/>

    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="{StaticResource TextColorBrush}"/>
    </Style>

    <Style TargetType="Grid">
        <Setter Property="Background" Value="{StaticResource Background}"/>
    </Style>
</ResourceDictionary>

In my main application, App.xaml I am referencing my 2 theme dictionaries as such

<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>

The way I am setting up the resources based on which theme I am choosing is done in the 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];
            }
        }
    }

And finally, my main window. Since I want these particular styles to be global I am not using any keys to identify them.

<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>

But my main issue is that the Label control doesn't show up in my application. I can see my background change color appropriately but my label control is just gone! What am I doing wrong? Many thanks in advance!

标签: c#wpfresourcedictionary

解决方案


不要将所有主题 ResourceDictionaries 从开头添加到Application.Resources.MergedDictionaries,即以空开头Application.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.Current.Resources.MergedDictionaries为当前主题来更改主题:

private void ChangeSkin(Skin skin)
{
    ResourceDictionary theme = null;

    switch (skin)
    {
        case Skin.Light:
            theme = new ResourceDictionary { Source = new Uri("pack://application:,,,/Foo.Core.WPF;component/Resources/Dictionary_LightTheme.xaml") };
            break;
        case Skin.Dark:
            theme = new ResourceDictionary { Source = new Uri("pack://application:,,,/Foo.Core.WPF;component/Resources/Dictionary_DarkTheme.xaml") };
            break;
    }

    if (theme != null)
    {
        Application.Current.Resources.MergedDictionaries.Clear();
        Application.Current.Resources.MergedDictionaries.Add(theme);
    }
}

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

<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>

那么你的主题 ResourceDictionaries 将只包含 Color 和 Brush 资源:

<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>

推荐阅读