为什么使用静态资源会破坏 Visual Studio 中的设计器?

Tim*_*oth 5 c# wpf xaml visual-studio

我在类库中有一个资源字典,其中有一堆 SolidColorBrush 和 Control 样式。当我确保我设置的所有控件都使用 DynamicResource 的样式时,它在设计器中起作用,但如果我将其切换为使用 StaticResource,则设计器会中断或我的应用程序无法运行...或两者兼而有之。

这有效

<Image Source="{Binding Image}" Style="{DynamicResource DriveImageStyle}"/>
Run Code Online (Sandbox Code Playgroud)

这打破了

<Image Source="{Binding Image}" Style="{StaticResource DriveImageStyle}"/>
Run Code Online (Sandbox Code Playgroud)

给我错误,例如:

XamlParseException:在“System.Windows.Markup.StaticResourceHolder”上提供值引发异常。

找不到名为“DriveImageStyle”的资源

使用资源字典本身中的资源时(设置样式的背景颜色)

这有效

<Setter Property="Background" Value="{DynamicResource ButtonBackgroundColour}" />
Run Code Online (Sandbox Code Playgroud)

这打破了

<Setter Property="Background" Value="{StaticResource ButtonBackgroundColour}" />
Run Code Online (Sandbox Code Playgroud)

并给我设计器中的错误,例如:

抛出异常:PresentationFramework.dll 中的“System.Windows.Markup.XamlParseException”

其他信息:“{DependencyProperty.UnsetValue}”不是属性“Background”的有效值。

谁能给我一个理由来解释为什么它会这样?

附加信息

在我的视图中,我像这样引用字典:

<UserControl.Resources>
        <ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
    </UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

我将它们合并到我的类库中,如下所示:

   <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Colours.xaml"/>
        <ResourceDictionary Source="Buttons.xaml"/>
        <ResourceDictionary Source="Controls.xaml"/>
    </ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)

Chr*_* W. 1

我希望我有时间去测试,但我想说这就是我上次直接从用户控件使用时所做的。不过,我通常建议反对它,并将其放在项目的 app.xaml 中或更集中的地方,除非该视图确实是唯一需要它的视图。不管怎样,请尝试一下,他们可能对细节很挑剔。

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)