如何在我的XAML中居中使用XML图形?

时间:2015-10-05 16:47:27

标签: xaml app.xaml

我是XAML的新手,我正在试图找出一些你定位的方法。我有一个以Adobe Illustrator图像开始的图形,然后我将其转换为可以呈现为XAML的代码。但是,我似乎无法将其显示在显示区域的中心位置。使用horizo​​ntalalignment,我可以毫无问题地将其他内容集中在一起,但出于某种原因,我似乎无法将此图形置于中心位置。我希望我错过了一些东西。

<Window x:Class="play.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Height="320">
        <Canvas x:Name="Layer_1" Canvas.Left="0" Canvas.Top="0" Width="62" Height="22" HorizontalAlignment="Center" Background="#00AEEF">
            <!--Unknown tag: metadata-->
            <!--Unknown tag: sodipodi:namedview-->
            <Canvas x:Name="g3" HorizontalAlignment="Center">
                <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="path5" Fill="White" Margin="0,0,0,0" HorizontalAlignment="Center">
                    <Path.Data>
                        <PathGeometry Figures="M16.733 8.245c-0.027-0.413-0.393-0.726-0.798-0.698c-0.413 0.027-0.726 0.385-0.698 0.798   c0.123 1.855-0.554 3.68-1.855 5.006c-1.199 1.225-2.783 1.899-4.46 1.899c0 0 0 0-0.001 0c-1.553 0-3.007-0.59-4.125-1.656   l1.177-1.177c0.28-0.279 0.081-0.758-0.314-0.758H2.38c-0.245 0-0.444 0.199-0.444 0.444v3.278c0 0.396 0.478 0.594 0.758 0.314   l1.041-1.041c1.401 1.35 3.231 2.096 5.185 2.096c0.001 0 0.001 0 0.002 0c2.082 0 4.047-0.835 5.53-2.349   C16.053 12.771 16.884 10.527 16.733 8.245z" FillRule="NonZero"/>
                    </Path.Data>
                </Path>
                <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="path7" Fill="White" Margin="0,0,0,0">
                    <Path.Data>
                        <PathGeometry Figures="M2.075 10.503c0.413-0.031 0.723-0.391 0.692-0.804c-0.14-1.868 0.535-3.709 1.85-5.051   C5.819 3.424 7.403 2.75 9.079 2.75c1.594 0 3.085 0.618 4.213 1.736l-1.152 1.152c-0.28 0.28-0.082 0.759 0.314 0.759h3.278   c0.245 0 0.444-0.199 0.444-0.444V2.674c0-0.396-0.479-0.594-0.758-0.314l-1.066 1.065C12.941 2.024 11.074 1.25 9.079 1.25   c-2.083 0-4.048 0.834-5.533 2.348C1.93 5.248 1.101 7.513 1.272 9.811c0.029 0.395 0.358 0.694 0.747 0.694   C2.037 10.505 2.057 10.504 2.075 10.503z" FillRule="NonZero"/>
                    </Path.Data>
                </Path>
            </Canvas>
        </Canvas>
    </Viewbox>

</StackPanel>

2 个答案:

答案 0 :(得分:0)

也许您应该使用网格(Grid.RowDefinitions和Grid.ColumnDefinitions)来使画布居中而不是Horizo​​ntalAlignment属性

祝你好运!

答案 1 :(得分:0)

Canvas is only good for static placement of graphics. You should start using grids and stackpanels.

I have modified your xaml:

<StackPanel>
    <Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Height="320">
        <Grid x:Name="Layer_1" Width="62" Height="22" HorizontalAlignment="Center" Background="#00AEEF">
            <!--Unknown tag: metadata-->
            <!--Unknown tag: sodipodi:namedview-->
            <Grid x:Name="g3" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="path5" Fill="White" Margin="0,0,0,0" HorizontalAlignment="Center">
                    <Path.Data>
                        <PathGeometry Figures="M16.733 8.245c-0.027-0.413-0.393-0.726-0.798-0.698c-0.413 0.027-0.726 0.385-0.698 0.798   c0.123 1.855-0.554 3.68-1.855 5.006c-1.199 1.225-2.783 1.899-4.46 1.899c0 0 0 0-0.001 0c-1.553 0-3.007-0.59-4.125-1.656   l1.177-1.177c0.28-0.279 0.081-0.758-0.314-0.758H2.38c-0.245 0-0.444 0.199-0.444 0.444v3.278c0 0.396 0.478 0.594 0.758 0.314   l1.041-1.041c1.401 1.35 3.231 2.096 5.185 2.096c0.001 0 0.001 0 0.002 0c2.082 0 4.047-0.835 5.53-2.349   C16.053 12.771 16.884 10.527 16.733 8.245z" FillRule="NonZero"/>
                    </Path.Data>
                </Path>
                <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="path7" Fill="White" Margin="0,0,0,0">
                    <Path.Data>
                        <PathGeometry Figures="M2.075 10.503c0.413-0.031 0.723-0.391 0.692-0.804c-0.14-1.868 0.535-3.709 1.85-5.051   C5.819 3.424 7.403 2.75 9.079 2.75c1.594 0 3.085 0.618 4.213 1.736l-1.152 1.152c-0.28 0.28-0.082 0.759 0.314 0.759h3.278   c0.245 0 0.444-0.199 0.444-0.444V2.674c0-0.396-0.479-0.594-0.758-0.314l-1.066 1.065C12.941 2.024 11.074 1.25 9.079 1.25   c-2.083 0-4.048 0.834-5.533 2.348C1.93 5.248 1.101 7.513 1.272 9.811c0.029 0.395 0.358 0.694 0.747 0.694   C2.037 10.505 2.057 10.504 2.075 10.503z" FillRule="NonZero"/>
                    </Path.Data>
                </Path>
            </Grid>
        </Grid>
    </Viewbox>

</StackPanel>