微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

这有效

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

这打破了

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

给我错误,如:

XamlParseException: Provide value on
‘System.Windows.Markup.StaticResourceHolder’ threw an exception.

Cannot find resource named ‘DriveImageStyle’

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

这有效

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

这打破了

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

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

Exception thrown: ‘System.Windows.Markup.XamlParseException’ in
PresentationFramework.dll

Additional @R_775_4045@ion: ‘{DependencyProperty.UnsetValue}’ is not a
valid value for property ‘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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐