我看到.Net XamlWriter在Silverlight中不可用。好吧 - 无论如何我需要一个,所以我假设有一个解决方案......?
我有一些UIElement对象(Path,Ellipse,Rectangle,..),我想存储他们的Xaml定义,以便稍后可以使用XamlWriter.Load()加载它们。关于如何做到这一点的任何想法?建议使用任何3rdParty XamlWriter实现等?
答案 0 :(得分: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();
}