WPF UserControl 在引用项目中找不到 XAML 资源 [英] WPF UserControl cannot find XAML resource in referencing project

查看:77
本文介绍了WPF UserControl 在引用项目中找不到 XAML 资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WPF 项目中,我将用户控件保存在一个单独的库项目中.用户控件访问单独的 XAML 文件中的资源,如下所示:

In my WPF project i keep a user control in a separate library project. The user control accesses resources in a separate XAML file, like this:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/ViewResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Local styles here -->
    </ResourceDictionary>
</UserControl.Resources>

资源文件 ViewResources.xaml 位于名为 Resources 的控件库项目中的文件夹中.它具有默认构建操作 (Page) 和自定义工具 (MSBuild:Compile).

The resource file, ViewResources.xaml, resides in a folder in the control library project named Resources. It has the default build action (Page) and custom tool (MSBuild:Compile).

问题是当我在 WPF 应用程序中引用控件库并使用用户控件时.在运行时,我得到以下 XamlParseException:

The problem is when I reference the control library in my WPF application and use the user control. At runtime, I get the following XamlParseException:

Set property 'System.Windows.ResourceDictionary.Source' threw an exception.

...包装IOException:

...which wraps the IOException:

Cannot locate resource 'resources/viewresources.xaml'.

我该如何解决这个问题?我试图将资源文件的构建操作更改为内容"并将其复制到输出目录(适用于文件和类似的哑"资源).但无济于事.此外,它在用户控件中不起作用.

How can I fix this? I have tried to change the resource file's build action to "content" and have it copied to the output directory (that works for files and similar "dumb" resources). But to no avail. Also, it doesn't work property in the user control then.

有没有更好的方法来指定路径?

Is there a better way to specify the path?

我是否必须将资源文件移动到应用程序项目中(我宁愿不这样做,因为它属于用户控件的域).

Will I have to move the resource file to the application project (I'd rather not, as it belongs in the user control's domain).

推荐答案

找到了.

事实证明有一种更好的方法来指定路径,打包 URI.我将 XAML 更改为以下内容:

Turns out there is a better way to specify the path, Pack URIs. I changed the XAML to the following:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/RoutingManager;component/Resources/ViewResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Local styles here -->
    </ResourceDictionary>
</UserControl.Resources>

然后就解决了.

这篇关于WPF UserControl 在引用项目中找不到 XAML 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆