【发布时间】:2011-01-11 09:32:11
【问题描述】:
我有一个包含 MainWindow 的 WPF 应用程序,它只显示一个 MainUserControl,它还包含带有合并字典的 App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\Styles.xaml"/>
<ResourceDictionary Source="Resources\DataTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
然后我决定在另一个项目中使用 MainUserControl,它应该显示在 ElementHost 中。不幸的是,它不起作用,因为找不到资源字典中的样式和 DataTemplate。我不知道解决它的正确方法,但我决定我可以将代码 ResourceDictionaries 代码移动到 MainUserControl 的资源中:
<UserControl.Resources>
<ResourceDictionary>
<!-- the same code -->
</ResourceDictionary>
</UserControl.Resources>
它导致我在资源字典中使用的转换器出错 - 无法创建未知类型 '{clrnamespace:MyApplication.Converters;assembly=MyApplication}LengthToStringConverter .我试图通过在我的转换器中再添加一个 ResourceDictionary 来将转换器移到资源字典之外:
<ResourceDictionary>
<con:LengthToStringConverter x:Key="textConverter"/>
<con:DateToTextConverter x:Key="dateConverter"/>
</ResourceDictionary>
它还导致了 xaml 异常(在“System.Windows.StaticResourceExtension”上提供值引发了异常。)。
所以我正在寻找的是解决在资源字典中找不到的转换器问题的方法,或者解决在另一个项目中使用 MainUserControl 类的问题的另一种方法(它根本找不到资源,也许,有办法指定它们?)。
【问题讨论】: