如何在“Canvas”中设置“BitmapIcon”或“PathIcon”内容存储在资源字典中?
<ResourceDictionary ...>
<Canvas x:Key="appbaricon" Height=77 Width=77>
<Path Fill="#FF000000" Data="F1 M 25.3333,42.75C 26.5189, ..."/>
</Canvas>
</ResourceDictionary>
...
<BitmapIcon ???Content???="{StaticResource appbaricon}">
有没有办法使用资源字典中的Canvas设置BitmapIcon的内容?如:
<AppBarButton Label="BitmapIcon" Click="AppBarButton_Click">
<AppBarButton.Icon>
<BitmapIcon ???Content???="{StaticResource appbaricon}">
</AppBarButton.Icon>
</AppBarButton>
答案 0 :(得分:1)
资源可以是任何可共享的对象,例如样式,模板,画笔和颜色。但是,控件,形状和其他FrameworkElements
不可共享,因此无法将它们声明为可重用资源。有关共享的详细信息,请参阅XAML resources must be shareable部分。
Canvas
是一个不可共享的控件,您不能直接将其用作一个资源。作为一种解决方法,我建议您创建自己的AppBarButton
样式作为资源并进行自定义。在新的AppBarButton
样式中,您可以将Icon
的默认内容替换为您在上面定义的Canvas
,Icon
由ViewBox
控件定义名为ContentViewbox
的样式。例如:
<Page.Resources>
<Style x:Key="AppBarButtonStyle1" TargetType="AppBarButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="AppBarButton">
<Grid x:Name="Root" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" MinWidth="{TemplateBinding MinWidth}" MaxWidth="{TemplateBinding MaxWidth}">
...
<Grid x:Name="ContentRoot" MinHeight="{ThemeResource AppBarThemeMinHeight}">
...
<Viewbox x:Name="ContentViewbox" AutomationProperties.AccessibilityView="Raw" HorizontalAlignment="Stretch" Height="20" Margin="0,14,0,4">
<ContentPresenter x:Name="Content" Foreground="{TemplateBinding Foreground}" Height="20">
<Canvas Height="77" Width="77">
<Path Data="F1 M 16,12 20,2L 20,16 1,16" Fill="#FF000000"/>
</Canvas>
</ContentPresenter>
</Viewbox>
...
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<AppBarButton Style="{StaticResource AppBarButtonStyle1}" Label="PathIcon" />
</Grid>