I create diagrams including my own custom node class. When I try to load that diagram in another project, I get an InvalidOperationException with the message "Failed to instantiate item of type 'my:DiagramCustomNode'."
What do I have to do so the importing diagram knows about that missing class?
I save and load the diagram document with SaveToString and LoadFromString.
This is my custom node class:
public class DiagramCustomNode : TemplatedNode
{
static DiagramCustomNode()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(DiagramCustomNode), new FrameworkPropertyMetadata(typeof(DiagramCustomNode)));
}
public DiagramCustomNode()
{
}
// Required for creating nodes by dragging them from the NodeListView
public DiagramCustomNode(DiagramCustomNode prototype)
: base(prototype)
{
Title = prototype.Title;
FullName = prototype.FullName;
Image = prototype.Image;
}
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
"Title",
typeof(string),
typeof(DiagramCustomNode),
new PropertyMetadata(""));
public string FullName
{
get { return (string)GetValue(FullNameProperty); }
set { SetValue(FullNameProperty, value); }
}
public static readonly DependencyProperty FullNameProperty = DependencyProperty.Register(
"FullName",
typeof(string),
typeof(DiagramCustomNode),
new PropertyMetadata(""));
public ImageSource Image
{
get { return (ImageSource)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
"Image",
typeof(ImageSource),
typeof(DiagramCustomNode),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
// serialization support for custom properties
protected override void SaveToXml(XmlElement xmlElement, XmlPersistContext context)
{
base.SaveToXml(xmlElement, context);
context.WriteString(Title, "Title", xmlElement);
context.WriteString(FullName, "FullName", xmlElement);
context.WriteImage(Image, "Image", xmlElement);
}
protected override void LoadFromXml(XmlElement xmlElement, XmlPersistContext context)
{
base.LoadFromXml(xmlElement, context);
Title = context.ReadString("Title", xmlElement);
FullName = context.ReadString("FullName", xmlElement);
Image = context.ReadImage("Image", xmlElement);
}
// undo and redo support
protected override DiagramItemProperties CreateProperties()
{
return new DiagramCustomNodeProperties();
}
protected override void SaveProperties(DiagramItemProperties props)
{
base.SaveProperties(props);
var state = (DiagramCustomNodeProperties)props;
state.Title = Title;
state.FullName = FullName;
state.Image = Image;
}
protected override void RestoreProperties(DiagramItemProperties props)
{
base.RestoreProperties(props);
var state = (DiagramCustomNodeProperties)props;
Title = state.Title;
FullName = state.FullName;
Image = state.Image;
}
}
public class DiagramCustomNodeProperties : DiagramNodeProperties
{
internal string Title;
internal string FullName;
internal ImageSource Image;
}