首页 > 解决方案 > 在 WPF 应用程序中添加 XAML 矢量图形

问题描述

我使用 inkscape 将 .svg 文件转换为 .xaml,生成的文件是画布。我将整个画布添加到文件 ImageResources.xaml 中的资源中:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Canvas x:Key="xamltest" Name="svg2" Width="9354.3341" Height="5977.5567"> 
<Canvas.RenderTransform> 
    <TranslateTransform X="0" Y="0"/>
...
...
...
</Canvas>
</ResourceDictionary>

然后我将资源文件合并到资源字典中:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/AP_PlugIn;component/Resources/BrushLists.xaml" />
        <ResourceDictionary Source="/AP_PlugIn;component/Resources/System/ConverterResources.xaml" />
        <ResourceDictionary Source="/AP_PlugIn;component/Resources/ImageResources.xaml"/>

  </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

最后,我尝试使用 ContentControl 在 Grid 中显示画布,但图像没有出现。有什么问题?我没有收到任何错误,我的项目正确启动

<Grid>
<ContentControl Content="{StaticResource xamltest}" Width="1253" Height="637"  /> 
</Grid>

标签: c#wpfxaml

解决方案


查看画布的尺寸,我怀疑图像的可见部分正在从屏幕上绘制出来。

尝试将画布换成网格,而不指定其大小。

以下最小的概念证明工作正常

<Window.Resources>
    <Grid x:Key="MyImage">
        <Path
            Data="M 0,0 M 100,100 M 25,50 L 50,25 L 75,50 L 50,75 z"
            Stretch="Fill"
            Stroke="Blue"
            StrokeThickness="5" />
        <Path
            Data="M 0,0 M 100,100 M 25,25 L 25,75 L 75,75 L 75,25 z"
            Stretch="Fill"
            Stroke="Black"
            StrokeThickness="10" />
    </Grid>
</Window.Resources>

<Grid
    Margin="16"
    Background="AliceBlue">

    <ContentControl Content="{StaticResource MyImage}" />

</Grid>

推荐阅读