加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:28:57 所属栏目:百科 来源:网络整理
导读:我在类库中有一个资源字典,它有一堆SolidColorBrush和Control样式.当我确保我设置的样式的所有控件都使用DynamicResource时,它在设计器中工作,但如果我将其切换为使用StaticResource,则设计器中断或我的应用程序无法运行……或两者兼而有之. 这有效 Image So
我在类库中有一个资源字典,它有一堆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 information: ‘{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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读