这似乎是一个老问题:为什么我无法从合并的字典中引用App.xaml中的StaticResource?以下是我的代码:
App.xaml:
App.xaml:
<Application x:Class="WpfResources.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<Color x:Key="MyColor">GreenYellow</Color>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="Button.Static.Foreground" Color="{StaticResource MyColor}"/>
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground"
Value="{StaticResource Button.Static.Foreground}"/>
</Style>
</ResourceDictionary>
在设计时,一切都很好,我可以看到按钮的前景设置为 MyColor。但在运行时,我遇到了以下问题:
异常:找不到名为 'MyColor' 的资源。资源名称区分大小写。
顺便说一句:这些代码在 UWP 中可以工作,但在 WPF 中似乎有所改变。我在网上做了很多搜索,但没有找到答案。
非常感谢任何想法!谢谢!
(顺便说一句:我不想改为 DynamicResource 解决方案)
编辑: 为什么有人给我打低分?有什么好的理由吗? 虽然这是一个“旧”的问题,但根据我的搜索,它仍然没有适当的答案!!