3

I see that the .Net XamlWriter is not available in Silverlight. Well - I need one anyway, so I assume there is a solution to this..?

I have some UIElement objects (Path, Ellipse, Rectangle, ..), and I want to store their Xaml definition such that I can load these later using XamlWriter.Load(). Any ideas on how to do this? Any 3rdParty XamlWriter implementations etc that are recommended?

stiank81
  • 25,418
  • 43
  • 131
  • 202

1 Answers1

3

There seems to be some implementations of XamlWriter for Silverlight around. The one I've seen which looks most serious is in Silverlight Contrib, but this is not yet supported for SL3, which I'm using.

Since I only had a few specific objects to extract xaml from I created the functions to do so myself. Some more refactoring will be done, but this one works for finding the xaml for my path drawing - stored as a 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();
    }
stiank81
  • 25,418
  • 43
  • 131
  • 202