当用户控件在另一个程序集中引用时,用户控件中引用的资源不会出现

发布于 2024-09-18 06:12:50 字数 1054 浏览 9 评论 0原文

我有 3 个程序集:

编号 1-ResourceAssembly,其中包含一个子文件夹 Images,其中包含 1 个图像(构建操作 = 资源)。该子文件夹中有一个 ResourceDictionary,其构建操作设置为资源。资源字典包含以下内容 -

<BitmapImage x:Key="BluePlus_48x48_72" UriSource="112_Plus_Blue_48x48_72.png"/>

数字 2 - 一个 wpf 控件程序集,其中包含一个名为 UserControl1 的用户控件。在该用户控件的资源中,我从程序集 1 加载资源字典并使用其中的图像。

<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/ResourcesAssembly;component/Images/ImagesDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

<Image Source="{DynamicResource BluePlus_48x48_72}" Height="48" Width="48"/>

图像正确显示。

第 3 号 - 主要应用程序组件。在 MainWindow.xaml 中,我引用 Project1 程序集并插入 UserControl1。

xmlns:p1="clr-namespace:Project1;assembly=Project1"
<p1:UserControl1 Height="60" Width="60"/>

但是,图像没有出现。没有编译时错误,也没有运行时输出到输出窗口的任何错误。我不知道为什么图像没有出现。有什么想法吗?

感谢您的帮助!

I have 3 assemblies:

Number 1- ResourceAssembly which contains a subfolder Images with 1 image in it (build action = resource). Within that subfolder is a ResourceDictionary with build action set to resource. The resource dictionary contains the following-

<BitmapImage x:Key="BluePlus_48x48_72" UriSource="112_Plus_Blue_48x48_72.png"/>

Number 2 - A wpf control assembly with a single usercontrol called UserControl1. Within that usercontrol's resources, I am loading the resource dictionary from assembly 1 and using the image in it.

<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/ResourcesAssembly;component/Images/ImagesDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

<Image Source="{DynamicResource BluePlus_48x48_72}" Height="48" Width="48"/>

The image appears correctly.

Number 3 - The main application assembly. Within the MainWindow.xaml, I reference the Project1 assembly and insert a UserControl1.

xmlns:p1="clr-namespace:Project1;assembly=Project1"
<p1:UserControl1 Height="60" Width="60"/>

However, the image does not appear. There are no compile-time errors nor any errors ouput to the Output window at runtime. I am at a loss as to why the image isn't appearring. Any thought?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-09-25 06:12:50

我认为您还需要在主应用程序中添加对“ResourceAssembly”项目的引用。或者至少将“ResourceAssembly.dll”与主应用程序的可执行文件放在同一文件夹中。

I think you'll need to also add a reference to the "ResourceAssembly" project in your main application. Or at least have the "ResourceAssembly.dll" in the same folder as your main application's executable.

衣神在巴黎 2024-09-25 06:12:50

尝试提供如下资源参考。

<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/YourAssembly.Name;component/Images/ImagesDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

<Image Source="{StaticResource BluePlus_48x48_72}" Height="48" Width="48"/>

注意:另外,不要忘记 Source="/ 中的 /

。HTH

Try giving the resource reference like below.

<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/YourAssembly.Name;component/Images/ImagesDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

<Image Source="{StaticResource BluePlus_48x48_72}" Height="48" Width="48"/>

Note: Also, don't forgot the / in Source="/ .

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文