使用ResourceDictionary(WPF)的App.xaml中出现“值不能为空”错误

时间:2019-03-04 16:48:45

标签: c# wpf xaml

使用.NET Framework 4.6.1,我使用的是通过NuGet安装的a UI kit,并且在项目中正确引用了它们。

App.xaml

<Application x:Class="ExampleApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:adonisUi="clr-namespace:AdonisUI;assembly=AdonisUI"
             xmlns:local="clr-namespace:ExampleApp"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.DarkColorScheme}" />
                <ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.ClassicTheme}" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml

<Window x:Class="ExampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ExampleApp"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">
    <Window.Style>
        <Style TargetType="Window"
               BasedOn="{StaticResource {x:Type Window}}" />
    </Window.Style>
    <Grid>
        <StackPanel Margin="10">
            <Button Content="Click Me"
                    HorizontalAlignment="Center" />
        </StackPanel>
    </Grid>
</Window>

问题:

<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.DarkColorScheme}" />
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.ClassicTheme}" />

App.xaml中的两行都出现以下错误:

Value cannot be null. Parameter name: item.

我尝试了多个新项目,进行了构建和重建,但不断出现此错误。我可以构建项目,并且即使错误仍然存​​在,也可以从UI套件中正确应用MainWindow的样式中看到样式。

但是样式没有显示在设计器窗口中,我不确定它是否与我收到的错误有关。

有什么想法会导致这种情况吗?

1 个答案:

答案 0 :(得分:2)

我刚刚尝试过,它对我也有相同的作用...使用Visual Studio Enterprise 2017 15.9.7

如果您在反编译器中查看AdonisUI.ResourceLocator(我使用Telerik的JustDecompile),则会看到以下定义:

public static Uri ClassicTheme
{
    get
    {
        return new Uri("pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml", UriKind.Absolute);
    }
}

public static Uri DarkColorScheme
{
    get
    {
        return new Uri("pack://application:,,,/AdonisUI;component/ColorSchemes/Dark.xaml", UriKind.Absolute);
    }
}

public static Uri LightColorScheme
{
    get
    {
        return new Uri("pack://application:,,,/AdonisUI;component/ColorSchemes/Light.xaml", UriKind.Absolute);
    }
}

如果您使用这些值将App.xaml更改为引用,那么它将起作用。

<ResourceDictionary Source="pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml" />
<ResourceDictionary Source="pack://application:,,,/AdonisUI;component/ColorSchemes/Dark.xaml" />