RecourceDictionaries 和自定义转换器
我有一个包含 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 中。不幸的是,它不起作用,因为找不到资源字典中的样式和数据模板。我不知道解决这个问题的正确方法,但我决定将代码 ResourceDictionaries 代码移动到 MainUserControl 的资源中:
<UserControl.Resources>
<ResourceDictionary>
<!-- the same code -->
</ResourceDictionary>
</UserControl.Resources>
它导致我在资源字典中使用的转换器出现错误 - 无法创建未知类型 '{clrnamespace:MyApplication.转换器;程序集=MyApplication}LengthToStringConverter 。我尝试通过在转换器中添加一个 ResourceDictionary 将转换器移至资源字典之外:
<ResourceDictionary>
<con:LengthToStringConverter x:Key="textConverter"/>
<con:DateToTextConverter x:Key="dateConverter"/>
</ResourceDictionary>
它还导致了 xaml 异常(在“System.Windows.StaticResourceExtension”上提供值引发了异常。)。
所以我正在寻找的是解决在资源字典中找不到的转换器问题的方法,或者解决在另一个项目中使用 MainUserControl 类的问题的另一种方法(它根本找不到资源,也许,那里是指定它们的方法吗?)。
I have a WPF Application containing MainWindow which just display a MainUserControl and it also contains App.xaml with Merged dictionaries:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\Styles.xaml"/>
<ResourceDictionary Source="Resources\DataTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then I decided to use MainUserControl in another project where it should be displayed in the ElementHost. Unfortunately, it doesn’t work because styles and DataTemplates from the resource dictionaries are not found. I don’t know the right way to solve it out but I decided that I may move code ResourceDictionaries code into the MainUserControl’s resources:
<UserControl.Resources>
<ResourceDictionary>
<!-- the same code -->
</ResourceDictionary>
</UserControl.Resources>
It caused the error with my converters I used in Resource dictionaries - Cannot create unknown type '{clrnamespace:MyApplication.Converters;assembly=MyApplication}LengthToStringConverter
. I tried to move converters outside the resource dictionaries by adding one more ResourceDictionary with my converters:
<ResourceDictionary>
<con:LengthToStringConverter x:Key="textConverter"/>
<con:DateToTextConverter x:Key="dateConverter"/>
</ResourceDictionary>
It also caused a xaml exception (Provide value on 'System.Windows.StaticResourceExtension' threw an exception.).
So what I’m looking for is the way to solve problem with converters which could not be found in the resource dictionaries or another way to solve problem with using MainUserControl class in another project (it can’t find resources at all, perhaps, there is a way to specify them?).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您已经在主项目中定义了资源。这意味着您只能在该项目内使用资源。如果您随后在另一个项目中使用
MainUserControl
,则该项目不知道主项目及其资源 - 这就是发生错误的原因。如果您想在另一个应用程序/项目中重用您的资源,您应该将所有资源移动到单独的基础程序集/项目中。然后,您可以从两个应用程序/项目引用此基础项目,以便可以从两个应用程序加载资源。
It seems like you have defined your resources in your main project. That means that you can use the resources only within this project. If you then use your
MainUserControl
in another project, this project does not know the main project and its resources - that's why the error occurred.If you want to reuse your resources in another application/project, you should move all your resources to a separate base assembly/project. You can then reference this base project from both applications/projects, so that the resources can be loaded from both applications.