Silverlight XamlWriter

2023-12-12

我发现 .Net XamlWriter 在 Silverlight 中不可用。好吧 - 无论如何我都需要一个,所以我认为有一个解决方案......?

我有一些 UIElement 对象(路径、椭圆、矩形等),并且我想存储它们的 Xaml 定义,以便稍后可以使用 XamlWriter.Load() 加载它们。关于如何做到这一点有什么想法吗?有推荐的 3rdParty XamlWriter 实现等吗?


似乎有一些针对 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();
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Silverlight XamlWriter 的相关文章

随机推荐