dependency properties do not get serialized

I have written the code shown below to serialize a string property in my custom activity. However, the property is not written to the XML file, when I call WorkflowMarkupSerializer.Serialize(). What am I missing?

--

[Serializable]

[DesignerSerializer(typeof(PanedActivitySerializer),typeof(WorkflowMarkupSerializer))]

[Designer(typeof(PanedActivityDesigner),typeof(IDesigner))]

[Designer(typeof(PanedActivityDesigner),typeof(IRootDesigner))]

publicclassPanedActivity : System.Workflow.ComponentModel.Activity

{

public PanedActivity()

:base()

{

}

public PanedActivity(string predicate)

:base()

{

this.Predicate = predicate ;

}

publicstaticDependencyProperty PredicateProperty =

System.Workflow.ComponentModel.DependencyProperty.Register(

"Predicate",typeof(string),typeof(PanedActivity));

publicstring Predicate

{

get

{

return ((string)(base.GetValue(PanedActivity.PredicateProperty)));

}

set

{

base.SetValue(PanedActivity.PredicateProperty,value);

}

}

}

publicclassPanedActivitySerializer :WorkflowMarkupSerializer

{

protectedoverridebool CanSerializeToString(System.Workflow.ComponentModel.Serialization.WorkflowMarkupSerializationManager serializationManager,object value)

{

returntrue;

}

protectedoverridestring SerializeToString(System.Workflow.ComponentModel.Serialization.WorkflowMarkupSerializationManager serializationManager,object value)

{

List<PropertyInfo> props =newList<PropertyInfo>(base.GetProperties(serializationManager, value));

return props[0].GetValue(value,null).ToString();

}

protectedoverrideobject DeserializeFromString(WorkflowMarkupSerializationManager serializationManager,Type propertyType,string value)

{

string predicate = value.ToString();

returnnewPanedActivity (predicate.ToString());

}

}

}

[11827 byte] By [mario.muja] at [2007-12-24]
# 1

Try re-writing your properties like this :

public static DependencyProperty BodyProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Body", typeof(string), typeof(SendMailActivity));


[
Description("Email body")]
[
Category("EmailActivity")]
[
Browsable(true)]
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string Body
{
get{ return ((string)(base.GetValue(SendMailActivity.BodyProperty))); }
set { base.SetValue(SendMailActivity.BodyProperty, value); } }

DesignerSerializationVisibility

Visible, Hidden, or Content

Determines how and if properties will be serialized.

Visible (the default) – the property will be serialized normally.

Hidden – prevents property serialization

Content – used for collection properties. The collection object itself is not serialized, however the contents of the collection are.

If a developer chooses a collection type, this property will be set to Content. If a developer chooses a non-serializable type, this property will be set to Hidden.

This example is from the custom activities hands-on lab available here for RC1 - http://wf.netfx3.com/files/folders/documentation/entry4919.aspx
BabyGBear at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 2

Hi Mario,

I tried the following out with the PanedActivity and it worked as expected:

public class Program
{
public static void Main()
{
WorkflowMarkupSerializer markupSerializer = new WorkflowMarkupSerializer();
PanedActivity testActivity = new PanedActivity("Hello World");
FileStream fs = new FileStream(@"C:\Out.xml", FileMode.Create);
using(XmlTextWriter xmlWriter = new XmlTextWriter(fs, Encoding.Default))
{
markupSerializer.Serialize(xmlWriter, testActivity);
}
}
}

Out.xml:

<ns0:PanedActivity x:Name="PanedActivity" Predicate="Hello World" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ns0="clr-namespace:ActivityLibrary1;Assembly=ActivityLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

Are you trying something different and may I ask you what build number of Windows Workflow Foundation are you using?

VishalKalra at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 3

Thanks for your help. I am using RC1. After adding the DesignerSerializationVisibility attribute, it worked.

Strange, that Vishal got the string property serialized without this attribute.

mario.muja at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...

Software Development for Windows Vista

Site Classified