在C#中设计GUI时,在xaml文件中,我创建了一个这样的形状:
<Style x:Key="Data" TargetType="Rectangle" BasedOn="{StaticResource FlowChartRectangleStyle}">
</Style>
<Style x:Key="Data_DragThumb" TargetType="Rectangle" BasedOn="{StaticResource Data}">
<Setter Property="IsHitTestVisible" Value="true"/>
<Setter Property="Height" Value="300"/>
<Setter Property="Width" Value="200"/>
</Style>
<Rectangle Style="{StaticResource Data}" ToolTip="Data" StrokeThickness="2">
<s:DesignerItem.DragThumbTemplate>
<ControlTemplate>
<Rectangle Style="{StaticResource Data_DragThumb}" x:Name="MyShape"/>
</ControlTemplate>
</s:DesignerItem.DragThumbTemplate>
</Rectangle>
到目前为止,此GUI工作正常,使我能够将形状保存在xml文件中。为了将输出保存为xml,代码的序列化部分如下所示:
XElement serializedItems = new XElement("ConfigurationItems",
from item in designerItems
let contentXaml = XamlWriter.Save(((DesignerItem)item).Content)
select new XElement("ConfigurationItem",
new XElement("X", Canvas.GetLeft(item)),
new XElement("Y", Canvas.GetTop(item)),
new XElement("Width", item.Width),
new XElement("Height", item.Height),
new XElement("ID", item.ID),
new XElement("zIndex", Canvas.GetZIndex(item)),
new XElement("ParentID", item.ParentID),
new XElement("Content", contentXaml)
)
);
输出xml文件是:
<Root>
<ConfigurationItems>
<ConfigurationItem>
<X>256</X>
<Y>190.54</Y>
<Width>130</Width>
<Height>117</Height>
<ID>fef14b95-4abb-44ea-a298-26d6e16a765e</ID>
<zIndex>0</zIndex>
<ParentID>00000000-0000-0000-0000-000000000000</ParentID>
<Content><Rectangle StrokeThickness="2" ToolTip="Data" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dd="clr-namespace:DiagramDesigner;assembly=GUI"><Rectangle.Style><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="Shape.Fill"><Setter.Value><LinearGradientBrush StartPoint="0,0" EndPoint="0,1"><LinearGradientBrush.GradientStops><GradientStop Color="#FFFAFBE9" Offset="0" /><GradientStop Color="#FFFFA500" Offset="1" /></LinearGradientBrush.GradientStops></LinearGradientBrush></Setter.Value></Setter><Setter Property="Shape.Stroke"><Setter.Value><SolidColorBrush>#FFD69436</SolidColorBrush></Setter.Value></Setter><Setter Property="Shape.StrokeThickness"><Setter.Value><s:Double>1</s:Double></Setter.Value></Setter><Setter Property="Shape.StrokeLineJoin"><Setter.Value><x:Static Member="PenLineJoin.Round" /></Setter.Value></Setter><Setter Property="Shape.Stretch"><Setter.Value><x:Static Member="Stretch.Fill" /></Setter.Value></Setter><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>False</s:Boolean></Setter.Value></Setter><Setter Property="UIElement.SnapsToDevicePixels"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources></Style></Rectangle.Style><dd:DesignerItem.DragThumbTemplate><ControlTemplate><Rectangle Name="MyShape"><Rectangle.Style><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="Shape.Fill"><Setter.Value><LinearGradientBrush StartPoint="0,0" EndPoint="0,1"><LinearGradientBrush.GradientStops><GradientStop Color="#FFFAFBE9" Offset="0" /><GradientStop Color="#FFFFA500" Offset="1" /></LinearGradientBrush.GradientStops></LinearGradientBrush></Setter.Value></Setter><Setter Property="Shape.Stroke"><Setter.Value><SolidColorBrush>#FFD69436</SolidColorBrush></Setter.Value></Setter><Setter Property="Shape.StrokeThickness"><Setter.Value><s:Double>1</s:Double></Setter.Value></Setter><Setter Property="Shape.StrokeLineJoin"><Setter.Value><x:Static Member="PenLineJoin.Round" /></Setter.Value></Setter><Setter Property="Shape.Stretch"><Setter.Value><x:Static Member="Stretch.Fill" /></Setter.Value></Setter><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>False</s:Boolean></Setter.Value></Setter><Setter Property="UIElement.SnapsToDevicePixels"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter><Setter Property="FrameworkElement.Height"><Setter.Value><s:Double>300</s:Double></Setter.Value></Setter><Setter Property="FrameworkElement.Width"><Setter.Value><s:Double>200</s:Double></Setter.Value></Setter></Style></Rectangle.Style></Rectangle></ControlTemplate></dd:DesignerItem.DragThumbTemplate></Rectangle></Content>
</ConfigurationItem>
</ConfigurationItems>
<Connections />
</Root>
现在我想做两件事:
第一个目标,我想将x:Name
属性值添加为XAttribute
XElement
,以便保存的xml文件看起来像这样:
<Root>
<ConfigurationItems>
<ConfigurationItem Name="MyShape">
<X>256</X>
<Y>190.54</Y>
... ...
为此,我尝试在我的代码中添加以下行来序列化项目:
XElement serializedItems = new XElement("ConfigurationItems",
from item in designerItems
let contentXaml = XamlWriter.Save(((DesignerItem)item).Content)
select new XElement("ConfigurationItem",
new XAttribute(" Name", item.Name.ToString()),
... ...
但是这为输出xml文件中的'Name'属性提供了一个完全空白的值,如下所示:
<Root>
<ConfigurationItems>
<ConfigurationItem Name="">
<X>256</X>
<Y>190.54</Y>
... ...
第二个目标,我想让用户能够更改GUI中的Name
标记值。例如,我想要的是,默认情况下,Name
标记应该等于我的xaml文件中定义的形状的x:Name
属性。但是当用户右键单击形状并选择一个项目(可能称为“添加新名称”)时,会出现一个文本框,用户输入“Rocket”这样的名称,在画布上创建另一个相同的形状并将其命名为“Aircraft” ,那么我希望xml输出文件看起来像:
<Root>
<ConfigurationItems>
<ConfigurationItem Name="Rocket">
<X>256</X>
<Y>190.54</Y>
... ...
</ConfigurationItem>
<ConfigurationItem Name="Aircraft">
... ...
有人可以帮助我解决这两个问题吗?提前谢谢了。