0
votes

I have tried to serialize all the child controls of the form using XamlServices.Save method. If i use the empty Datagridview which is working fine, but assign the datasource for datagridview which throws following exception,

System.MissingMethodException was unhandled HResult=-2146233069
Message=Constructor on type 'System.ComponentModel.ComponentConverter' not found. Source=mscorlib StackTrace: at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, Object[] args) at System.Xaml.Schema.SafeReflectionInvoker.CreateInstanceCritical(Type type, Object[] arguments) at System.Xaml.Schema.SafeReflectionInvoker.CreateInstance(Type type, Object[] arguments) at System.Xaml.Schema.XamlValueConverter 1.CreateInstance() at System.Xaml.Schema.XamlValueConverter 1.get_ConverterInstance() at System.Xaml.XamlObjectReader.TypeConverterExtensions.GetConverterInstance[TConverter](XamlValueConverter`1 converter) at System.Xaml.XamlObjectReader.SerializerContext.TryValueSerializeToString(ValueSerializer valueSerializer, TypeConverter propertyConverter, SerializerContext context, Object& value) at System.Xaml.XamlObjectReader.MemberMarkupInfo.GetPropertyValueInfoInternal(Object propertyValue, ValueSerializer propertyValueSerializer, TypeConverter propertyConverter, Boolean isXamlTemplate, XamlMember xamlProperty, SerializerContext context) at System.Xaml.XamlObjectReader.MemberMarkupInfo.GetPropertyValueInfo(Object propertyValue, XamlMember xamlProperty, SerializerContext context) at System.Xaml.XamlObjectReader.MemberMarkupInfo.ForReadWriteProperty(Object source, XamlMember xamlProperty, SerializerContext context) at System.Xaml.XamlObjectReader.MemberMarkupInfo.ForProperty(Object source, XamlMember property, SerializerContext context) at System.Xaml.XamlObjectReader.ObjectMarkupInfo.AddRecordMembers(Object value, SerializerContext context, ParameterInfo[] methodParameters, XamlType xamlType) at System.Xaml.XamlObjectReader.ObjectMarkupInfo.AddRecordMembers(Object value, SerializerContext context, TypeConverter converter) at System.Xaml.XamlObjectReader.ObjectMarkupInfo.ForObjectInternal(Object value, SerializerContext context, TypeConverter converter) at System.Xaml.XamlObjectReader.ObjectMarkupInfo.ForObject(Object value,at System.Threading.ThreadHelper.ThreadStart() InnerException:

Code

private void DemonstrateTheSerializationException()
{
   // let's serialize this form to a file
   var sFullPathFileName = "test.xml";
   using (TextWriter writer = File.CreateText(sFullPathFileName))
   {
       XamlServices.Save(writer, this);
   }
}

Am i use this method for serialization or am i missed anything here?

Thanks in advance,

1

1 Answers

0
votes

You should not use it for WPF stuff.

"You should continue to use XamlReader.Load and XamlWriter.Save for WPF types. WPF has a number of things in it that aren't supported directly in the lower, more general purpose, System.Xaml.dll APIs. This is for .NET 4, not just Beta2 of .NET 4."

Use this:

var result=System.Windows.Markup.XamlWriter.Save(this.LayoutGrid);

Consider cleaning your xaml graph to only include the things you really need before calling XamlServices.Save.

In my case some FrameworkElements had objects in the Tag property - and those would not serialize.

I did this to handle it:

public string NullOutAllTagValuesAndSerializeXaml()
{
  IterateXamlNullOutTag(this.LayoutGrid);
  return System.Xaml.XamlServices.Save(this.LayoutGrid);
}

private void IterateXamlNullOutTag(FrameworkElement fe)
{
  fe.Tag = null;
  var c=VisualTreeHelper.GetChildrenCount(fe);
  for (int i = 0; i < c; i++)
  {
    var fechild=VisualTreeHelper.GetChild(fe, i) as FrameworkElement;
    if (fechild != null)
      IterateXamlNullOutTag(fechild);
  }
}

Considering cleaning up your object in a similar fashion