UWP ResourceDictionary的样式错误:参数不正确
我做的共同风格的ResourceDictionary是贯穿我的应用程序中使用,其中之一是:UWP 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="" />
</Setter.Value>
</Setter>
</Style>
这一切ok,如果我申请的风格只有一个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).
无论是AppBarButton
一个Icon property或FontIcon从UIElement
派生,所以我想这就是为什么不能将此属性在资源字典中风格的原因。
此外,我会考虑如果这是一个正确的方向来定义样式中的每个AppBarButton
的属性,通常我想给每个按钮一个不同的图标作为内容。
但是,如果你坚持这样做,我可以为您提供通过定义AppBarButton
的Content
一个变通办法,这是你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="" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
真棒 它的工作原理就像一个魅力:) 此外,谢谢你详尽的解释,为什么我的路不可能工作...我还没有收到有关这部分的想法:“作为自定义类型资源不能在继承中拥有UIElement类' 非常感谢您 –
我测试了你的代码,它工作正常。请分享你的资源字典和App.xaml中 – RicardoPons