我的 App.Resources MergedDictionary 遇到了一个棘手的问题。每次添加带有来自另一个程序集的源和 XML 命名空间的新字典时,都会产生错误,并且我无法构建我的程序。
该错误出现在 App.xaml 中,并带有下划线的 ResourceDictionary 和消息Value Cannot be Null. Parameter Name: item。这完全没有意义,因为我以前也做过同样的事情。唯一的区别是,这次我引用了另一个程序集(c# 类库)的命名空间。这是 App.xaml 文件:
<Application x:Class="Quiz.Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Quiz.Client"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles.xaml" />
<ResourceDictionary Source="Resources/Templates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
下面是 Styles.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Quiz.Client">
<Style x:Key="StrongFont"
TargetType="ContentControl">
<Setter Property="FontSize"
Value="20" />
<Setter Property="FontWeight"
Value="ExtraBold" />
<Setter Property="Foreground"
Value="DarkRed" />
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
这是 Templates.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Quiz.Client"
xmlns:domain="clr-namespace:Quiz.Core.Domain;assembly=Quiz.Core"
xmlns:common="clr-namespace:Quiz.Client.Common">
<DataTemplate x:Key="ArithmeticQuestionTemplate"
DataType="domain:ArithmeticQuestion">
</DataTemplate>
<common:QuestionTemplateSelector x:Key="QuestionTemplateSelector"
ArithmeticTemplate="{StaticResource ArithmeticQuestionTemplate}" />
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
到底他妈发生了什么?是否引入了软件破坏性更改或其他内容?我完全迷失了。
不知道为什么你的方法不起作用,但这不是我通常自己导入资源的方式。通常,我会将 ResourceDictionary 添加到我的库项目中,将所有库资源放入其中,然后使用 pack 语法将其导入到我的主项目中:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyLibrary;component/LibraryResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- main project resources go here -->
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
总的来说,我认为这是一个更好的架构,因为每次子库资源之一发生变化时,它不需要重新编译主项目。如果您决定切换到 MEF,以后也会更容易,因为您的库资源现在封装在一个您可以轻松使用的 ResourceDictionary 对象中[Export]。
| 归档时间: |
|
| 查看次数: |
4120 次 |
| 最近记录: |