gpt4 book ai didi

wpf - 使用键从 ResourceDictionary 中获取资源

转载 作者:行者123 更新时间:2023-12-04 14:03:55 25 4
gpt4 key购买 nike

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

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

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Images/LCCD logo.xaml" />
<ResourceDictionary Source="Images/LCCD bottom image.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>


<Image Source="{Binding Source={StaticResource LCCDlogo}}" /> <!-- Simplified -->
<Image Source="{Binding Source={StaticResource LCCDbar}}" /> <!-- Simplified -->

但是,我现在需要向 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>

警告

The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection.



所以我改了 <ResourceDictionary>标记为:
<ResourceDictionary x:Key="Images">

但是,我现在不知道如何访问此字典中的资源。您如何从名为 ResourceDictionary 的内部获取资源?

编辑

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

错误是:

''Resources' property has already been set on 'MainWindow'.



我想我将不得不以其他方式做到这一点。

最佳答案

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

It is legal to define resources within a ResourceDictionary that is specified as a merged dictionary, either as an alternative to specifying Source, or in addition to whatever resources are included from the specified source. However, this is not a common scenario; the main scenario for merged dictionaries is to merge resources from external file locations. If you want to specify resources within the markup for a page, you should typically define these in the main ResourceDictionary and not in the merged dictionaries.



所以试试这个,看看它是否有效。
<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>

关于wpf - 使用键从 ResourceDictionary 中获取资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10924556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com