Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) ControlNode Serialization (Read 4028 times)
marksolo
YaBB Newbies
*
Offline


Suffering suckertash.....
..

Posts: 18
Joined: Nov 26th, 2008
ControlNode Serialization
Apr 1st, 2009 at 11:19pm
Print Post  
Hello,

I am having trouble serializing a class I inherited from ControlNode.  Does any one have any similar experiences or examples?

Any other threads that might be relavent?

Thanks for any pointers.

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


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: ControlNode Serialization
Reply #1 - Apr 2nd, 2009 at 9:34am
Print Post  
Hi,

To enable serialization of custom node types, call the RegisterItemClass method when your application initializes, e.g. in Form_Load:

Diagram.RegisterItemClass(typeof(MyControlNode), "MyControlNode", 1);

Now when saving nodes from your type, they will be preceded by "MyControlNode" in the binary stream if using binary serialization, and their <Node> element will have that id as an attribute if using XML serialization. When loading, the control will create an instance of the MyControlNode type when it sees the "MyControlNode" id. To enable instantiating your control, you should also provide either a no-args constructor or one that takes a Diagram argument.

If your class adds any custom fields, you must also override the SaveTo/LoadFrom methods for binary serialization or SaveToXml/LoadFromXml for XML serialization and add code to save or load the fields.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
marksolo
YaBB Newbies
*
Offline


Suffering suckertash.....
..

Posts: 18
Joined: Nov 26th, 2008
Re: ControlNode Serialization
Reply #2 - Apr 2nd, 2009 at 4:13pm
Print Post  
Thanks for the quick reply Stoyo,

Actually I did all that, and my custom control nodes saves and loads (binary stream) without creating any errors or exceptions.  It simply does not draw my custom control after loading. 

Thru a property grid, I can see that my user control was instantiated after loading, all the properties are there, but you just can't see it.  In other words, all you see is the shadow drawn where the ControlNode should be.  If I set the Control property to another control like a textBox (after reloading), it still does not draw the textbox.

Will it help is I post the source code?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: ControlNode Serialization
Reply #3 - Apr 2nd, 2009 at 5:18pm
Print Post  
Is the Diagram associated with any DiagramView at the time it's being loaded?
  
Back to top
 
IP Logged
 
marksolo
YaBB Newbies
*
Offline


Suffering suckertash.....
..

Posts: 18
Joined: Nov 26th, 2008
Re: ControlNode Serialization
Reply #4 - Apr 2nd, 2009 at 5:51pm
Print Post  

Hi Stoyo,

I think you may have a handle on the problem,
the diagram was indeed loaded before the form is loaded,
I added these codes before loading but it did not help, in fact the diagram property was not null.

if (this.diagramView1.Diagram == null){
       this.diagramView1.Diagram=this.diagram1;
}

Anyway I will try to modify the code so I load the form first, its going to be a lot of work but I guess I have to do it if there are no workarounds.

Mark
  
Back to top
 
IP Logged
 
marksolo
YaBB Newbies
*
Offline


Suffering suckertash.....
..

Posts: 18
Joined: Nov 26th, 2008
Re: ControlNode Serialization
Reply #5 - Apr 2nd, 2009 at 8:24pm
Print Post  
???

Well, I modified the code so I Show the form first, which I assume will link the diagram and the diagram view for sure, then I call serialization to load my ControlNode, the same problem still persists.

Any ideas?
« Last Edit: Apr 2nd, 2009 at 9:47pm by marksolo »  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: ControlNode Serialization
Reply #6 - Apr 3rd, 2009 at 7:51am
Print Post  
Could you try the following:

Add a constructor to your node class that takes a DiagramView argument, and calls the corresponding base ControlNode constructor. Add the following to yout init code:

IItemFactory factory = diagram;
factory.InstantiateItem += new InstantiateItemEventHandler(OnInstantiateItem);

and handle the event like this:

private void OnInstantiateItem(object sender, InstantiateItemEventArgs e)
{
     if (e.Item == null && e.ItemClass == "MyControlNode")
           e.Item = new MyControlNode(this);
}
  
Back to top
 
IP Logged
 
marksolo
YaBB Newbies
*
Offline


Suffering suckertash.....
..

Posts: 18
Joined: Nov 26th, 2008
Re: ControlNode Serialization
Reply #7 - Apr 3rd, 2009 at 3:53pm
Print Post  
Hi Stoyo,

Here are my two constructors, one for diagram and one for diagramView, and I implemented the event handler as follows:    


       public PPICtrlNode(Diagram diagram)
           : base(diagram)
       {
           m_diagram = diagram;
           IItemFactory factory = diagram;
           factory.InstantiateItem += new InstantiateItemEventHandler(OnInstantiateItem);
       }


   public PPICtrlNode(DiagramView diagramView)
           : base(diagramView)
       {
           m_diagram = diagramView.Diagram;
           IItemFactory factory = m_diagram;
           factory.InstantiateItem += new InstantiateItemEventHandler(OnInstantiateItem);
       }

       void OnInstantiateItem(object sender, InstantiateItemEventArgs e)
       {
           if (e.Item == null && e.ItemClass == "PPICtrlNode")
               e.Item = (DiagramItem)(new PPICtrlNode(this.m_diagram));
       }


Unfortunately the result is the same
  
Back to top
 
IP Logged
 
marksolo
YaBB Newbies
*
Offline


Suffering suckertash.....
..

Posts: 18
Joined: Nov 26th, 2008
Re: ControlNode Serialization
Reply #8 - Apr 3rd, 2009 at 5:30pm
Print Post  
more information....

If I serialize two of my custom ControlNode,
both is redrawn with shadows only, both can be resized and moved about, properties modified thru a property window, and original groupings also persists.

but when you try to delete the first one, a node not found exception occurs, the second one can be deleted without any problem. another strange thing is the Group that contains the first node, also can not be found in the diagram's group collection, even though it is in the diagram!

since no one else seems to have this problem i wonder what i did to cause this strange behavior
  
Back to top
 
IP Logged
 
marksolo
YaBB Newbies
*
Offline


Suffering suckertash.....
..

Posts: 18
Joined: Nov 26th, 2008
Re: ControlNode Serialization
Reply #9 - Apr 3rd, 2009 at 8:03pm
Print Post  

now i am getting the following exception,


************* Exception Text **************
System.Runtime.Serialization.SerializationException: Type 'MindFusion.Diagramming.WinForms.ControlNode' in Assembly 'MindFusion.Diagramming.WinForms, Version=5.2.0.34294, Culture=neutral, PublicKeyToken=21bf4734c13246ea' is not marked as serializable.

This is very strange, I was trying something and suddenly i am getting this messge, surely ControlNode should be serializable, no?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: ControlNode Serialization
Reply #10 - Apr 6th, 2009 at 8:33am
Print Post  
Hi Mark,

Check this example:
https://mindfusion.eu/_samples/MyControlNodes.zip

We don't use the .NET's serialization system because of some versioning problems we found while trying it, so the classes are not marked [Serializable].

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