C# 为什么使用静态资源会破坏VisualStudio中的设计器?
我在类库中有一个资源字典,它有一堆SolidColorBrush和控件样式。当我确保我正在设置其样式的所有控件都使用DynamicSource时,它在设计器中工作,但如果我将其切换为使用StaticResource,则设计器会中断,或者我的应用程序无法运行。。。或者两者兼而有之 这很有效C# 为什么使用静态资源会破坏VisualStudio中的设计器?,c#,wpf,visual-studio,xaml,C#,Wpf,Visual Studio,Xaml,我在类库中有一个资源字典,它有一堆SolidColorBrush和控件样式。当我确保我正在设置其样式的所有控件都使用DynamicSource时,它在设计器中工作,但如果我将其切换为使用StaticResource,则设计器会中断,或者我的应用程序无法运行。。。或者两者兼而有之 这很有效 这打破了 给我一些错误,例如: XamlParseException:提供上的值 “System.Windows.Markup.StaticResourceHolder”引发异常 找不到名为“Drive
这打破了
给我一些错误,例如:
XamlParseException:提供上的值
“System.Windows.Markup.StaticResourceHolder”引发异常
找不到名为“DriveImageStyle”的资源
使用资源字典中的资源时(设置样式的背景色)
这很有效
这打破了
并给出设计器中的错误,例如:
引发异常:中的“System.Windows.Markup.XamlParseException”
PresentationFramework.dll
其他信息:“{DependencyProperty.UnsetValue}”不是
属性“背景”的有效值
有人能给我一个理由解释它为什么会这样做吗
补充资料
在我看来,我是这样引用字典的:
<UserControl.Resources>
<ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
</UserControl.Resources>
我将它们合并到我的类库中,如下所示:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colours.xaml"/>
<ResourceDictionary Source="Buttons.xaml"/>
<ResourceDictionary Source="Controls.xaml"/>
</ResourceDictionary.MergedDictionaries>
我希望我有时间去测试,但我想说的是,这就是我上次直接从用户控件使用它时所做的。然而,我通常会建议不要使用它,只需在项目的app.xaml或其他更集中的地方使用它,除非只有一个视图真正需要它。不管是哪种方式,给他们一次机会,他们可能会对细节感到紧张
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
对于第一部分,我认为这是因为它在类库中,而不仅仅是在项目中,所以它必须动态地定位它。我在一个项目中用一个资源字典进行了尝试,静态和动态都很好。对于第二部分,如果在使用静态资源之前必须定义它们,那么这可能是问题所在,那么您如何在资源字典中的合并词典中引用它?给我们看看你的包URI。@ChrisW。我已经用你要求的信息更新了我的问题
<Setter Property="Background" Value="{StaticResource ButtonBackgroundColour}" />
<UserControl.Resources>
<ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
</UserControl.Resources>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colours.xaml"/>
<ResourceDictionary Source="Buttons.xaml"/>
<ResourceDictionary Source="Controls.xaml"/>
</ResourceDictionary.MergedDictionaries>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>