UWP ResourceDictionary Style Error: The parameter is incorrect

UWP ResourceDictionary Style Error: The parameter is incorrect

我正在制作一个 ResourceDictionary,其中包含在我的整个应用程序中使用的常用样式,其中之一是:

<Style x:Key="ME_BASE_AppbarButtonSaveStyle"
       TargetType="AppBarButton">
    <Setter Property="Label"
            Value="Save" />
    <Setter Property="ToolTipService.ToolTip"
            Value="Save" />
    <Setter Property="Icon">
        <Setter.Value>
            <FontIcon FontFamily="Segoe MDL2 Assets"
                      Glyph="&#xE105;" />
        </Setter.Value>
    </Setter>
</Style>

如果我在页面上只应用一个AppbarButton的样式是可以的,但是如果我想有两个相同样式的按钮,我会得到以下错误:

The parameter is incorrect

如果我从样式中删除图标 属性 就可以了(没有错误)... 但这有点忽略了重点...

有人经历过类似的事情吗?或许...

感谢大家的帮助。

Error HRESULT E_Fail has been returned from a call to a COM component.

第二次使用此样式时会出现此错误AppBarButton。这个错误一般发生在引用不存在或者不在XAML的上下文中的样式或者事件处理程序时,可以看到你的问题的异常信息:

如果您阅读此文档:XAML resources must be shareable,您会发现:

Custom types used as resources can't have the UIElement class in their inheritance, because a UIElement can never be shareable (it's always intended to represent exactly one UI element that exists at one position in the object graph of your runtime app).

是否 Icon property of AppBarButton or a FontIcon 派生自 UIElement,所以我想这就是为什么不能在资源字典中设置此 属性 样式的原因。

此外,我会考虑为样式中的每个 AppBarButton 定义 Icon 属性 是否是正确的方向,通常我想给每个按钮一个不同的图标作为内容。

但是如果你坚持要这样做,我可以通过定义AppBarButtonContent来给你一个变通的方法,这是你的AppBarButton的构造:

你使用了一个FontIcon作为AppBarButton的内容,所以我们可以这样修改你的样式:

<Style x:Key="ME_BASE_AppbarButtonSaveStyle" TargetType="AppBarButton">
    <Setter Property="Label" Value="Save" />
    <Setter Property="ToolTipService.ToolTip" Value="Save" />
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <FontIcon FontFamily="Segoe MDL2 Assets"
              Glyph="&#xE105;" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>