一步一步,我正在优化我的xaml以从图像创建自定义AppBarButton。我已经从一个完整的自定义xaml布局变为使用样式的几行,但我知道我可以简化这一步。
以下是我目前的情况:
<Style x:Key="PrintAppBarButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="PrintAppBarButton"/>
<Setter Property="AutomationProperties.Name" Value="Print"/>
</Style>
<Button Style="{StaticResource PrintAppBarButtonStyle}">
<ContentControl>
<Image Source="/Assets/AppBar/appbar_printer_dark.png"/>
</ContentControl>
</Button>
我知道我可以将图像源移动到样式定义中,但我无法使其工作。在阅读了AppBarButton课程后,我尝试将 TargetType 设置为AppBarButton
,然后设置Icon
属性,但未成功。如下所示:
<Style x:Key="PrintAppBarButtonStyle" TargetType="AppBarButton" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="PrintAppBarButton"/>
<Setter Property="AutomationProperties.Name" Value="Print"/>
<Setter Property="Icon">
<Setter.Value>
// here it's expecting an IconElement
</Setter.Value>
</Setter>
</Style>
<Button Style="{StaticResource PrintAppBarButtonStyle}"/>
任何提示?
答案 0 :(得分:1)
Icon
属性不接受图片 - 如果您需要使用图片,请坚持使用Content
属性。也可以设置样式:
<Style x:Key="PrintAppBarButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="PrintAppBarButton"/>
<Setter Property="AutomationProperties.Name" Value="Print"/>
<Setter Property="Content">
<Setter.Value>
<ContentControl>
<Image Source="/Assets/AppBar/appbar_printer_dark.png"/>
</ContentControl>
</Setter.Value>
</Setter>
</Style>
作为解释,请注意“ContentControl.Content”是ContentProperty
,这意味着子元素被设置为“Content”。即,这个:
<Button Style="{StaticResource PrintAppBarButtonStyle}">
<ContentControl>
<Image Source="/Assets/AppBar/appbar_printer_dark.png"/>
</ContentControl>
</Button>
只是简写:
<Button Style="{StaticResource PrintAppBarButtonStyle}">
<Button.Content>
<ContentControl>
<Image Source="/Assets/AppBar/appbar_printer_dark.png"/>
</ContentControl>
</Button.Content>
</Button>