Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic How do I import diagrams with custom nodes? (Read 1612 times)
Centron
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 24
Joined: Feb 11th, 2015
How do I import diagrams with custom nodes?
Feb 25th, 2015 at 9:20am
Print Post  
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:

Code
Select All
    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;
    }
 

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How do I import diagrams with custom nodes?
Reply #1 - Feb 25th, 2015 at 11:11am
Print Post  
You will have to add the custom class to that other project too, possibly by implementing it in a shared assembly, and also call Diagram.RegisterItemClass(...  "my:DiagramCustomNode") before calling LoadFromString.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint