2009-08-10 46 views
3

我看到.Net XamlWriter在Silverlight中不可用。好吧 - 無論如何我需要一個,所以我認爲這是一個解決方案..?我有一些UIElement對象(路徑,橢圓,矩形,..),我想存儲他們的Xaml定義,以便我可以稍後使用XamlWriter.Load()加載這些對象。任何想法如何做到這一點?任何推薦的3rdParty XamlWriter實現等?Silverlight XamlWriter

回答

3

Silverlight似乎有一些XamlWriter的實現。我見過的看起來最嚴重的是Silverlight Contrib,但目前尚未支持SL3。

因爲我只有一些特定的對象來提取xaml從我創建的功能,我自己這樣做。還有一些重構會完成,但是這個工程可以用於查找我的路徑繪圖的xaml - 存儲爲InkPresenter:

public static string ConvertPathToXaml(InkPresenter drawObject) 
    { 
     string xmlnsString = "http://schemas.microsoft.com/client/2007"; 
     XNamespace xmlns = xmlnsString; 

     var strokes = new XElement(xmlns + "StrokeCollection");    

     foreach (var strokeData in drawObject.Strokes) 
     { 
      var stroke = new XElement(xmlns + "Stroke", 
       new XElement(xmlns + "Stroke.DrawingAttributes", 
        new XElement(xmlns + "DrawingAttributes", 
         new XAttribute("Color", strokeData.DrawingAttributes.Color), 
         new XAttribute("OutlineColor", strokeData.DrawingAttributes.OutlineColor), 
         new XAttribute("Width", strokeData.DrawingAttributes.Width), 
         new XAttribute("Height", strokeData.DrawingAttributes.Height))));       
      var points = new XElement(xmlns + "Stroke.StylusPoints"); 

      foreach (var pointData in strokeData.StylusPoints) 
      { 
       var point = new XElement(xmlns + "StylusPoint", 
        new XAttribute("X", pointData.X), 
        new XAttribute("Y", pointData.Y)); 
       points.Add(point); 
      } 
      stroke.Add(points); 
      strokes.Add(stroke); 
     } 

     var strokesRoot = new XElement(xmlns + "InkPresenter.Strokes", strokes); 
     var inkRoot = new XElement(xmlns + "InkPresenter", new XAttribute("xmlns", xmlnsString), 
      new XAttribute("Opacity", drawObject.Opacity), strokesRoot); 

     return inkRoot.ToString(); 
    }