Thanks for all the help in my previous post. I have another question! What is the correct way to programmatically create a diagram and assign it to a view?
Up until this point, I have created my Diagram in XAML like this:
<diag:DiagramView x:Name="diagramView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<diag:Diagram x:Name="diagram" Bounds="0, 0, 2000, 2000" />
</diag:DiagramView>
Then, in the initialization code of the page, I have some simple code like this to add a node to the diagram:
ShapeNode my_node = new ShapeNode(diagram);
my_node.Bounds = new Rectangle(10, 20, 40, 30);
diagram.Nodes.Add(my_node);
diagram.Behavior = MindFusion.Diagramming.Behavior.PanAndModify;
diagram.RouteLinks = true;
diagram.AlignToGrid = true;
diagram.ShowGrid = true;
diagram.GridSizeX = 10;
diagram.GridSizeY = 10;
diagram.GridStyle = GridStyle.Lines;
diagram.ShowAnchors = ShowAnchors.Always;
diagram.SnapToAnchor = SnapToAnchor.OnCreateOrModify;
The above code works. However, when I change the code to try to do creation/assignment of the Diagram programmatically, it no longer works. Here is an example that
does not work. The XAML now looks like this:
<diag:DiagramView x:Name="diagramView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
And the C# looks like this:
public partial class MainPage : ContentPage
{
private Diagram diagram = new Diagram();
public MainPage()
{
InitializeComponent();
ShapeNode my_node = new ShapeNode(diagram);
my_node.Bounds = new Rectangle(10, 20, 40, 30);
diagram.Nodes.Add(my_node);
diagram.Bounds = new Rectangle(0, 0, 2000, 2000);
diagram.Behavior = MindFusion.Diagramming.Behavior.PanAndModify;
diagram.RouteLinks = true;
diagram.AlignToGrid = true;
diagram.ShowGrid = true;
diagram.GridSizeX = 10;
diagram.GridSizeY = 10;
diagram.GridStyle = GridStyle.Lines;
diagram.ShowAnchors = ShowAnchors.Always;
diagram.SnapToAnchor = SnapToAnchor.OnCreateOrModify;
diagramView.Diagram = diagram;
}
}
Any suggestions on how to properly programmatically assign the Diagram to the DiagramView?
Edited: I have noticed that in the examples online, y'all do something like this:
diagram = diagramView.Diagram;
So you are effectively letting DiagramView initialize its own Diagram object, and then you are using that one. I have tested that, and technically it does work, but unfortunately I can't do that for my use case. In my use case I want to be able to swap in and out Diagram objects to a DiagramView.