繁体   English   中英

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

[英]Why does using static resources break the designer in Visual studio?

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

这有效

<Image Source="{Binding Image}" Style="{DynamicResource DriveImageStyle}"/>

这打破了

<Image Source="{Binding Image}" Style="{StaticResource DriveImageStyle}"/>

给我错误,如:

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

找不到名为'DriveImageStyle'的资源

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

这有效

<Setter Property="Background" Value="{DynamicResource ButtonBackgroundColour}" />

这打破了

<Setter Property="Background" Value="{StaticResource ButtonBackgroundColour}" />

并给我设计师的错误,如:

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

附加信息:'{DependencyProperty.UnsetValue}'不是属性'Background'的有效值。

任何人都可以给我一个理由,为什么它以这种方式行事?

附加信息

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

<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使用时的方式。 然而,我通常建议不要反对它,只是在项目的app.xaml或更集中的地方拍打它,除非一个视图真的是唯一需要它的视图。 无论哪种方式都给了这个镜头,他们可以对细节有所了解。

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM