如何使用矢量XAML文件?

时间:2014-05-03 00:45:53

标签: xaml windows-store-apps winrt-xaml

我有93个SVG文件,我使用XamlTune并跟随these instructions转换为XAML。生成的文件将矢量信息放入Canvas

我知道如何在我的应用程序中使用这些文件。我是否需要将它们放入ResourceDictionary中,还是可以直接从应用程序包中访问它们?

示例XAML文件:

<Canvas Name="Layer_1" Width="20" Height="20" ClipToBounds="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Canvas><Path Fill="#FF222222"><Path.Data><PathGeometry FillRule="Nonzero" Figures="M4.3,0.025C3.776,0.025,3.35,0.451,3.35,0.975L3.35,4.775C3.35,5.299 3.776,5.725 4.3,5.725 4.824,5.725 5.25,5.299 5.25,4.775L5.25,0.975C5.25,0.451,4.824,0.025,4.3,0.025z" /></Path.Data></Path><Path Fill="#FF222222"><Path.Data><PathGeometry FillRule="Nonzero" Figures="M15.7,0.025C15.176,0.025,14.75,0.451,14.75,0.975L14.75,4.775C14.75,5.299 15.176,5.725 15.7,5.725 16.224,5.725 16.65,5.299 16.65,4.775L16.65,0.975C16.65,0.451,16.224,0.025,15.7,0.025z" /></Path.Data></Path><Path Fill="#FF222222"><Path.Data><PathGeometry FillRule="Nonzero" Figures="M18.55,3.35L17.6,3.35 17.6,4.775C17.6,5.82 16.745,6.675 15.7,6.675 14.655,6.675 13.8,5.82 13.8,4.775L13.8,3.35 6.2,3.35 6.2,4.775C6.2,5.82 5.345,6.675 4.3,6.675 3.255,6.675 2.4,5.82 2.4,4.775L2.4,3.35 1.45,3.35C0.926,3.35,0.5,3.776,0.5,4.3L0.5,7.15 0.5,8.575 1.925,8.575 18.075,8.575 19.5,8.575 19.5,7.15 19.5,4.3C19.5,3.776,19.074,3.35,18.55,3.35z" /></Path.Data></Path><Path Fill="#FF222222"><Path.Data><PathGeometry FillRule="Nonzero" Figures="M0.5,9.525L0.5,19.025C0.5,19.549,0.926,19.975,1.45,19.975L18.55,19.975C19.074,19.975,19.5,19.549,19.5,19.025L19.5,9.525 0.5,9.525z M17.6,18.075L2.4,18.075 2.4,11.425 17.6,11.425 17.6,18.075z" /></Path.Data></Path></Canvas></Canvas>

1 个答案:

答案 0 :(得分:2)

我想我明白你在问什么。让我们开始在ResourceDictionary中使用它们非常好,可能是理想的。第二个最明显的用途是将它们放在UserControl中。这两个选项都允许您根据需要重复使用它们。

让我假装我有一个Canvas我想做到这一点。

<Canvas Height="100" Width="100" Background="Blue" />

现在,要使用ResourceDictionary,我会这样做:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Name="MyShape">
            <Canvas Height="100" Width="100" Background="Red" />
        </DataTemplate>
    </Grid.Resources>
    <ContentPresenter Content="{x:Null}" ContentTemplate="{StaticResource MyShape}" />
</Grid>
  

请注意,您可以将其从Resources中移除,并根据需要将其移至文件中。

而且,如果你想要一个能让你拥有代码隐藏优势的用户控件,你也可以这样做。

祝你好运。