从不同的程序集加载资源

时间:2013-11-08 07:25:54

标签: c# wpf xaml resourcedictionary

我有一个名为MyApp的WPF应用程序,它有一个名为MyApp.Infrastructure的独立程序集。 在MyApp.Infrastructure中,我有一个ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <BitmapImage x:Key="leftImage" UriSource="/Images/left.png" />
    <BitmapImage x:Key="errorImage" UriSource="/Images/error.png" />
</ResourceDictionary>

图像也在MyApp.Infrastructure中。

在MyApp中,我有以下app.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyApp.Infrastructure;component/ImageDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

现在我在MyApp.Infrastructure中有一个usercontrol,我想加载一个资源。 当我这样做时,它在设计器中工作,但不在运行时:

<Image Source="{StaticResource leftImage}" />

我还尝试在usercontrol中加载ResourceDictionary,但这不是helo。

知道我在这里做错了吗?

2 个答案:

答案 0 :(得分:2)

您可以尝试使用以下语言包语法:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MyApp.Infrastructure;component/ImageDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

不要忘记将文件的构建操作设置为Resource

答案 1 :(得分:1)

我将ResourceDictiony更改为:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <BitmapImage x:Key="leftImage" UriSource="/MyApp.Infrastructure;Component/Images/left.png" />
    <BitmapImage x:Key="errorImage" UriSource="/MyApp.Infrastructure;Component/Images/error.png" />
</ResourceDictionary>

在MyApp中搜索的图像似乎是因为我在app.xaml中加载了资源字典。

相关问题