GeometryDrawing serialization/deserialization
There is a need to provide some sort of interface(save/edit/load) to dynamically generated styles that are formed from GeometryDrawings. The problem is distinction between them in situation they lack Name property. I've tried something like this :
// some dummy predefined style, it has more drawings but i keep first one
const string templateXaml = @"<DrawingBrush xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" Stretch=""Uniform"">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Geometry=""F1 M 77,92L 704,92L 704,517L 77,517L 77,92 Z "" x:Name=""test_name"">
<GeometryDrawing.Pen>
<Pen Thickness=""4"" LineJoin=""Round"" B开发者_如何学编程rush=""#FFFF7D00""/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<!--More drawings here -->
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>";
var drawingBrush = XamlReader.Parse(templateXaml) as DrawingBrush;
var firstDrawing = (drawingBrush.Drawing as DrawingGroup).Children[0] as GeometryDrawing;
//..
var name = firstDrawing.GetValue(NameProperty);
//..
firstDrawing.SetValue(FrameworkElement.NameProperty, "some value");
I would like to have x:Name here in name variable (or something like this) to be able to change it and than serialize to get, for example, the same style but with different names for drawings (case is now they are named Element1,2,3..., and have to be named according to subject area, and that processing have to be automatic but not manual regarding the fact there is no ability to WYSIWYG'ly do that - each Drawing is put into Visual, Visuals are put into custom FrameworkElement etc.). Of course, the whole process of working with Drawings is a pain, but that is a system design requirement i can't ignore. Thanks in advance.
Didn't find better way than wrap drawings in BL classes and serialize/deserialize them. Lost consistency and violated requirements a bit - now i have two entities (plain geometries and classes that describes them) representing graphical objects, but they can be converted/interchanged when needed.
精彩评论