Click here to Skip to main content
12,948,130 members (88,841 online)
Click here to Skip to main content
Add your own
alternative version


Posted 24 Jan 2012

Use Xaml.XamlServices to Serialize or Deserialize DependencyObject

, 24 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use Xaml.XamlServices to serialize or deserialize a DependencyObject.

I was writing a Data Entity designer, and looking for a solution to serialize all objects (entities, models, fields, etc.) in the designer.

However, XamlWriter/XamlReader does not serialize UI objects (canvas, grid) properly. The content was lost and they did not generate object references.

Finally, I had to use XamlServices.Save/Parse.

I encountered the following two problems:

  1. Deferred contents can not be saved.

  2. Name, x:Name that were assigned or generated during serialization may cause an exception saying "Cannot register duplicate Name {0} in this scope" during deserialization.

Here is the solution:

  1. All objects should be defined by code and do not refer to any external sources (which are deferred objects). But images in resources are fine.

  2. In the root element/dependency object, a NameScope is required to hold all the names/reference IDs in the serialized contents.

For example, a NameScope was placed in the root element.

Public Class Designer
    Inherits Canvas
    Private dsNameScope As New NameScope
    Public Sub New()
        dsNameScope.Add("root", Me)
    End Sub
End Class

Furthermore, the content/child/children can be hidden from the serializer by Shadows.

Public Class Designer
    Inherits Canvas
    Private dsNameScope As New NameScope
    Private WithEvents _Items As New System.Collections.ObjectModel.ObservableCollection(Of ModelBase)
    Public Sub New()
        SetValue(ChildrenProperty, _Items)
        dsNameScope.Add("root", Me)
    End Sub
    Public Shadows ReadOnly Property Children As _
           System.Collections.ObjectModel.ObservableCollection(Of ModelBase)
            Return GetValue(ChildrenProperty)
        End Get
    End Property
    Public Shared ReadOnly ChildrenProperty As DependencyProperty = _
           DependencyProperty.Register("Children", _
           GetType(System.Collections.ObjectModel.ObservableCollection(Of ModelBase)), GetType(DataSet), _
           New PropertyMetadata(Nothing))
End Class


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jack Z. Stone
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 24 Jan 2012
Article Copyright 2012 by Jack Z. Stone
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid