Silverlight XamlWriter

I see .Net XamlWriter is not available in Silverlight. Well, I still need to, so I guess there is a solution to this ...?

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

+3
source share
1 answer

There seems to be some XamlWriter implementations for Silverlight. The one I saw that looks the most serious is in the Silverlight Contrib, but this is not yet supported for the SL3 that I am using.

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();
    }
+3

Source: https://habr.com/ru/post/1714775/


All Articles