2015-10-05 7 views
0

我是XAML的新手,我試圖找出一些定位事物的方法。我有一個圖形,最初是作爲Adobe Illustrator圖像開始的,然後我將它轉換爲可以像XAML一樣渲染的代碼。但是,我似乎無法讓它在顯示區域內居中顯示。使用橫向排列,我可以將其他事物排除在外,但由於某種原因,我似乎無法使此圖形居中。我希望我錯過了一些東西。如何在我的XAML中集中XML圖形?

<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> 

+0

什麼是你用你的路徑數據轉換?如果它是[Mike Swanson's](http://www.mikeswanson.com/xamlexport/)(這非常方便),那麼確保你將它導出爲正確的東西。在你做之前,我會合並/統一這兩個路徑,並將其作爲一條路徑導出,如果它正確完成,那麼它不應該關心大小,並且不應該需要ViewBox,因爲它是矢量。那麼你用什麼來導出路徑數據,並理解很多與它一起導出的東西是垃圾,你想在移入你的應用程序之前刪除。 –

+0

我在名爲Inkscape的應用程序中打開圖形,然後使用另存爲> Microsoft XAML。我沒有Adobe Illustrator,但鏈接的應用看起來很酷。我將保存未來的信息。我使用的URL是https://inkscape.org/en/download/ 我不確定如何合併這兩條路徑。我會看看我能否弄清楚。 感謝您的信息。 – Kevin

+0

結合路徑進行了幾次嘗試,但我得到了它的工作。我刪除了視框,但由於某種原因圖形仍保持對齊狀態。這裏沒有太多的代碼,所以它似乎應該很容易弄清楚,但我沒有任何運氣。我嘗試將內畫布對準中心,我嘗試將外畫布對準中心,但沒有喜悅。 – Kevin

回答

0

畫布僅適用於圖形的靜態放置。 您應該開始使用網格和堆棧面板。

我已經修改了你的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> 
+0

這個解決方案和我最終實現的主要區別在於,我遵循了Mike Swanson的建議(上圖),並複製了第二個路徑圖的值,並將其粘貼到第一個路徑的數值中,然後刪除第二個路徑的數值。這是一個很好的建議,我沒有考慮過。感謝大家的幫助! – Kevin

0

也許你應該使用一個網格(Grid.RowDefinitions和Grid.ColumnDefinitions)居中你的畫布,而不是Horizo​​ntalAlignment屬性

好運的!