2016-02-25 13 views
2

我做的共同风格的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="&#xE105;" /> 
     </Setter.Value> 
    </Setter> 
</Style> 

这一切ok,如果我申请的风格只有一个AppbarButton在页面上,但如果我想有一个具有相同风格的两个按钮,我收到以下错误:

The parameter is incorrect 

这是确定(没有错误),如果我删除图标地产出的风格.. 但是这是缺少点...

任何人都有类似的经历吗?也许...

谢谢你的所有帮助。

+1

我测试了你的代码,它工作正常。请分享你的资源字典和App.xaml中 – RicardoPons

回答

2

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

当您将此样式用于第二个AppBarButton时会发生此错误。这个错误通常发生在以一种风格或不存在或不是与XAML的上下文中的事件处理程序的引用,你可以看到你的问题的异常信息: enter image description here

如果你阅读本文件: 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 propertyFontIconUIElement派生,所以我想这就是为什么不能将此属性在资源字典中风格的原因。

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

但是,如果你坚持这样做,我可以为您提供通过定义AppBarButtonContent一个变通办法,这是你AppBarButton建设: enter image description here

您使用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> 
+0

真棒 它的工作原理就像一个魅力:) 此外,谢谢你详尽的解释,为什么我的路不可能工作...我还没有收到有关这部分的想法:“作为自定义类型资源不能在继承中拥有UIElement类' 非常感谢您 –

相关问题