2011-03-08 119 views
5

追蹤如何以編程方式將路徑字符串轉換爲WPF中的路徑對象並不難,但有沒有內置函數可將幾何或路徑轉換回迷你語言中的字符串?將幾何/路徑轉換爲Minilanguage字符串?

+0

我想不是,因爲它是相反的方式:迷你語言被解析成幾何。 – 2011-03-08 19:13:43

+1

「追蹤如何以編程方式將路徑字符串轉換爲WPF中的路徑對象並不困難」小心分享一些資源? – jpierson 2011-07-16 05:16:22

+0

當然! http://stackoverflow.com/questions/2029680/wpf-c-path-how-to-get-from-a-string-with-path-data-to-geometry-in-code-not-in – 2011-07-22 14:32:11

回答

6

編輯︰看着這個剛纔我認爲應該有一個類GeometryConverter應該能夠做到這一點,而且確實有。只需創建其中一個,然後在要轉換的幾何圖形上使用ConvertToString即可。


可以使用XamlWriter類輸出對象爲XAML,幾何會自動降低到小型語言。

例如如果這是你輸入:

<DrawingImage x:Name="segmentsDrawing"> 
    <DrawingImage.Drawing> 
     <DrawingGroup> 

      <GeometryDrawing Brush="Red"> 
       <GeometryDrawing.Pen> 
        <Pen Brush="Black" /> 
       </GeometryDrawing.Pen> 
       <GeometryDrawing.Geometry> 
        <PathGeometry> 
         <PathFigure StartPoint="100,100"> 
          <PathFigure.Segments> 
           <LineSegment Point="100,0"/> 
           <ArcSegment Point="186.6,150" SweepDirection="Clockwise" Size="100,100"/> 
           <LineSegment Point="100,100"/> 
          </PathFigure.Segments> 
         </PathFigure> 
        </PathGeometry> 
       </GeometryDrawing.Geometry> 
      </GeometryDrawing> 

      <GeometryDrawing Brush="Blue"> 
       <GeometryDrawing.Pen> 
        <Pen Brush="Black"/> 
       </GeometryDrawing.Pen> 
       <GeometryDrawing.Geometry> 
        <PathGeometry> 
         <PathFigure StartPoint="100,100"> 
          <PathFigure.Segments> 
           <LineSegment Point="186.6,150"/> 
           <ArcSegment Point="13.4,150" SweepDirection="Clockwise" Size="100,100"/> 
           <LineSegment Point="100,100"/> 
          </PathFigure.Segments> 
         </PathFigure> 
        </PathGeometry> 
       </GeometryDrawing.Geometry> 
      </GeometryDrawing> 

      <GeometryDrawing Brush="Green"> 
       <GeometryDrawing.Pen> 
        <Pen Brush="Black"/> 
       </GeometryDrawing.Pen> 
       <GeometryDrawing.Geometry> 
        <PathGeometry> 
         <PathFigure StartPoint="100,100"> 
          <PathFigure.Segments> 
           <LineSegment Point="13.4,150"/> 
           <ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/> 
           <LineSegment Point="100,100"/> 
          </PathFigure.Segments> 
         </PathFigure> 
        </PathGeometry> 
       </GeometryDrawing.Geometry> 
      </GeometryDrawing> 
     </DrawingGroup> 
    </DrawingImage.Drawing> 
</DrawingImage> 

...你序列化......

XmlTextWriter writer = new XmlTextWriter(@"C:\Users\Public\Test.xml", new UTF8Encoding()); 
writer.Formatting = Formatting.Indented; 
writer.Indentation = 1; 
writer.IndentChar = '\t'; 
XamlWriter.Save(segmentsDrawing, writer); 

...你會得到如下:

<DrawingImage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <DrawingImage.Drawing> 
     <DrawingGroup> 
      <DrawingGroup.Children> 
       <GeometryDrawing Brush="#FFFF0000"> 
        <GeometryDrawing.Pen> 
         <Pen Brush="#FF000000" /> 
        </GeometryDrawing.Pen> 
        <GeometryDrawing.Geometry> 
         <PathGeometry Figures="M100,100L100,0A100,100,0,0,1,186.6,150L100,100" /> 
        </GeometryDrawing.Geometry> 
       </GeometryDrawing> 
       <GeometryDrawing Brush="#FF0000FF"> 
        <GeometryDrawing.Pen> 
         <Pen Brush="#FF000000" /> 
        </GeometryDrawing.Pen> 
        <GeometryDrawing.Geometry> 
         <PathGeometry Figures="M100,100L186.6,150A100,100,0,0,1,13.4,150L100,100" /> 
        </GeometryDrawing.Geometry> 
       </GeometryDrawing> 
       <GeometryDrawing Brush="#FF008000"> 
        <GeometryDrawing.Pen> 
         <Pen Brush="#FF000000" /> 
        </GeometryDrawing.Pen> 
        <GeometryDrawing.Geometry> 
         <PathGeometry Figures="M100,100L13.4,150A100,100,0,0,1,100,0L100,100" /> 
        </GeometryDrawing.Geometry> 
       </GeometryDrawing> 
      </DrawingGroup.Children> 
     </DrawingGroup> 
    </DrawingImage.Drawing> 
</DrawingImage> 

所有PathGeometry現以微型語言。如果你想立即在你的應用程序中使用它,我想你可以把它寫入MemoryStream,並通過它創建一個XmlDocument來從中獲取數據。

+0

這是一個迂迴,但只是可能伎倆!謝謝... – 2011-04-16 23:07:33