使用密钥从ResourceDictionary获取资源

Mos*_*atz 6 wpf xaml resourcedictionary

我有一个WPF窗口,应该从XAML文件加载两个矢量图像.(每个都在一个单独的文件中,以便在Expression Design中更容易修改.)

当我将XAML文件包含在a中时MergedDictionary,它可以正常工作.这是我使用的代码:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Images/LCCD logo.xaml" />
            <ResourceDictionary Source="Images/LCCD bottom image.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

<Image Source="{Binding Source={StaticResource LCCDlogo}}" /> <!-- Simplified -->
<Image Source="{Binding Source={StaticResource LCCDbar}}" /> <!-- Simplified -->
Run Code Online (Sandbox Code Playgroud)

但是,我现在需要向Window的资源添加更多内容.新资源属于此窗口,因此我们希望它位于同一文件中而不是包含的文件中.

当我在<Window.Resources>和之间添加以下代码时<ResourceDictionary>,我收到以下错误:

<Style TargetType="{x:Type tab:FabTabItem}">
        <Setter Property="Header" Value="{Binding Path=LabelText}"/>
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0,0,4,0">
                        <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

警告

设计人员不支持加载混合"ResourceDictionary"项目的词典,而不包含同一集合中的键和其他项目.请确保'Resources'属性不包含没有键的'ResourceDictionary'项,或者'ResourceDictionary'项是该集合中唯一的元素.

所以我将<ResourceDictionary>标签更改为:

<ResourceDictionary x:Key="Images">
Run Code Online (Sandbox Code Playgroud)

但是,我现在不知道如何访问此Dictionary中的资源.如何从命名内部获取资源ResourceDictionary


编辑

没关系.这编译但不运行.

错误是:

''资源'属性已经在'MainWindow'上设置.

我想我必须以其他方式做到这一点.

Mar*_*all 9

根据MergedResourceDictionary上的MSDN页面,在MergedDictionary中指定的资源字典中定义资源是合法的,但并不常见.从上面的页面.

在指定为合并字典的ResourceDictionary中定义资源是合法的,或者作为指定Source的替代方法,或者除了指定源中包含的任何资源之外.但是,这不是常见的情况; 合并字典的主要方案是合并外部文件位置的资源.如果要在页面的标记中指定资源,通常应在主ResourceDictionary中定义这些资源,而不是在合并的字典中定义.

所以试试看它是否有效.

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Images/LCCD logo.xaml" />   
            <ResourceDictionary Source="Images/LCCD bottom image.xaml" />
            <ResourceDictionary>
                <Style TargetType="{x:Type tab:FabTabItem}">
                    <Setter Property="Header" Value="{Binding Path=LabelText}"/>
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="0,0,4,0">
                                    <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/>
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)