如何在WPF中使用存储在xaml文件中的图标?

时间:2014-11-13 02:03:31

标签: wpf xaml icons

抱歉我的英语不好。该图标存储在名为Icon.xaml的xaml文件中。我将该文件作为资源添加到项目中。

<ResourceDictionary x:Class="resources_icons_xaml"  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Canvas x:Key="appbar_acorn" Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0">
        <Path Width="22.3248" Height="25.8518" Canvas.Left="13.6757" Canvas.Top="11.4012" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 16.6309,18.6563C 17.1309,8.15625 29.8809,14.1563 29.8809,14.1563C 30.8809,11.1563 34.1308,11.4063 34.1308,11.4063C 33.5,12 34.6309,13.1563 34.6309,13.1563C 32.1309,13.1562 31.1309,14.9062 31.1309,14.9062C 41.1309,23.9062 32.6309,27.9063 32.6309,27.9062C 24.6309,24.9063 21.1309,22.1562 16.6309,18.6563 Z M 16.6309,19.9063C 21.6309,24.1563 25.1309,26.1562 31.6309,28.6562C 31.6309,28.6562 26.3809,39.1562 18.3809,36.1563C 18.3809,36.1563 18,38 16.3809,36.9063C 15,36 16.3809,34.9063 16.3809,34.9063C 16.3809,34.9063 10.1309,30.9062 16.6309,19.9063 Z "/>
    </Canvas>
</ResourceDictionary >        

现在,我想在窗口的标题栏中使用该图标。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

Window.IconImageSource。所以你不应该在这里使用Canvas。尝试将其更改为某些DrawingImage,如下所示:

<DrawingImage x:Key="appbar_acorn">
   <DrawingImage.Drawing>
      <GeometryDrawing Brush="{DynamicResource BlackBrush}" Geometry="F1 M 16.6309,18.6563C 17.1309,8.15625 29.8809,14.1563 29.8809,14.1563C 30.8809,11.1563 34.1308,11.4063 34.1308,11.4063C 33.5,12 34.6309,13.1563 34.6309,13.1563C 32.1309,13.1562 31.1309,14.9062 31.1309,14.9062C 41.1309,23.9062 32.6309,27.9063 32.6309,27.9062C 24.6309,24.9063 21.1309,22.1562 16.6309,18.6563 Z M 16.6309,19.9063C 21.6309,24.1563 25.1309,26.1562 31.6309,28.6562C 31.6309,28.6562 26.3809,39.1562 18.3809,36.1563C 18.3809,36.1563 18,38 16.3809,36.9063C 15,36 16.3809,34.9063 16.3809,34.9063C 16.3809,34.9063 10.1309,30.9062 16.6309,19.9063 Z">
      </GeometryDrawing>
   </DrawingImage.Drawing>
</DrawingImage>

然后将它用于您的Window:

<Window ...
        Icon="{StaticResource appbar_acorn}">
    <!-- ... -->
</Window>

如果您在Window范围内导入资源文件,则必须改为使用DynamicResource

<Window ...
        Icon="{DynamicResource appbar_acorn}">
    <!-- ... -->
</Window>