如何将活动属性中具有内部属性的自定义类型序列化到xaml中? [英] How to serialize an custom type with internal properties in an activity property into the xaml?
问题描述
我想使用代码示例会更容易理解:
I guess it will be easier to understand with a code sample :
// A custom activity with a custom property type with his editor
public class Class2 : System.Activities.NativeActivity
{
[Editor (typeof (TestParameterDefinitionEditor), typeof (DialogPropertyValueEditor))]
public Person TotoArgument { get; set; }
protected override void Execute (NativeActivityContext context)
{
}
}
// The sample editor :
public class TestParameterDefinitionEditor : System.Activities.Presentation.PropertyEditing.DialogPropertyValueEditor
{
public TestParameterDefinitionEditor ()
{
this.InlineEditorTemplate = new DataTemplate ();
FrameworkElementFactory editModeSwitch = new FrameworkElementFactory (typeof (EditModeSwitchButton));
editModeSwitch.SetValue (EditModeSwitchButton.TargetEditModeProperty, PropertyContainerEditMode.Dialog);
this.InlineEditorTemplate.VisualTree = editModeSwitch;
}
public override void ShowDialog (System.Activities.Presentation.PropertyEditing.PropertyValue propertyValue, System.Windows.IInputElement commandSource)
{
var value = new Person () { FullName = "Fullanme", TelephoneNumber = "6+5+6-54965", };
propertyValue.Value = value;
}
}
// The sample data class :
public class Person
{
[DataMember]
internal string FullName;
private string _telephoneNumberValue;
[DataMember (Name = "TelephoneNumberTestDataMember")]
[XmlAttribute ("TelephoneNumberTestXml")]
public string TelephoneNumber
{
get
{
return _telephoneNumberValue;
}
set
{
_telephoneNumberValue = value;
}
}
}
这是生成的xaml,并且我在工作流程设计器中使用编辑器:
And this is the generated xaml and i use the editor in the workflow designer :
<Activity mc:Ignorable="sap" x:Class="ConsoleApplication2.Activity1" mva:VisualBasic.Settings="Assembly references and imported namespaces serialized as XML namespaces" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:local="clr-namespace:ConsoleApplication2" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sd1="clr-namespace:System.Data;assembly=System.Data.DataSetExtensions" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<local:Class2 sad:XamlDebuggerXmlReader.FileName="C:\Users\emonda\Documents\Visual Studio 10\Projects\Solution1\ConsoleApplication2\Activity1.xaml" sap:VirtualizedContainerService.HintSize="200,200">
<local:Class2.TotoArgument>
<local:Person TelephoneNumber="6+5+6-54965" />
</local:Class2.TotoArgument>
</local:Class2>
</Activity>
我们可以看到,我有电话号码,但没有全名,因为它是内部的.
我尝试使用[DataContract]和[XmlAttribute]序列化方法来自定义序列化,但是他们似乎都没有改变任何东西.似乎只有带有get/set(或字段)的公共属性才被序列化.但是我的自定义类型具有很多内部属性.那么如何正确地对其进行序列化呢?
As we can see, I have the TelephoneNumber but not the FullName because it's internal.
I tryeid both the [DataContract] and [XmlAttribute] serialization method to be able to customize the serialisation, but none of them seem to change anything. Only public properties with get/set (or fields) seems to be serialized. But my custom type have lot of internals properties. So how can serialize it right?
推荐答案
Instriker,通过使用XML序列化,IXmlSerializable接口可以进行这种自定义.在那里,您可以定义自己的WHOLE Xml文档以进行序列化/反序列化...但是在WCF上,我想这不可能使框架能够根据WCF/etc的绑定选择最佳的序列化程序. ..
Instriker, working with XML Serialization, the IXmlSerializable interface allows for this kind of customzation. There you define the WHOLE Xml document to serialize/deserialize as you like... but on WCF I suppose this not possible in order to give the framework the ability to choose the best serializer according to the setup of the Bindings of WCF/etc...
这篇关于如何将活动属性中具有内部属性的自定义类型序列化到xaml中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!