<< Click to Display Table of Contents >> NShape Basic Tutorial Basic Tutorial: Libraries |
In this step you will learn how to add shape libraries to a new NShape project.
Currently the WebVisits application still loads the Circles diagram at start-up. That was just for demonstration and is actually completely unnecessary. What you actually want is not loading an existing project but creating a new one.
1. | Replace the line |
project1.Open(); |
by
project1.Create(); |
2. | Remove the lines |
project1.AutoLoadLibraries = true; |
and
display1.LoadDiagram("Diagram 1"); |
3. | Run the application. What you see now is an empty display and an empty toolbox. |
4. | When you select File > Load Statistics, you get an exception because the shape type Ellipse is not registered. |
Why is Ellipse not registered? That is because you now have a complete fresh project with no shape libraries attached. In NShape, each project must determine by itself, which types of shapes it requires and the default is no shapes at all.
Therefore, you somehow must get some shape types from a library and there are two ways to do that: Dynamic and static loading of shape types. Dynamic loading means that you can load assemblies that contain shape types (so-called shape libraries) at run-time and those libraries will then be added to the project. That is how the NShape Designer delivered with the NShape package works, because that enables you to add your own shape libraries if you wish so.
Static loading means that the shape library is referenced from the project. The assembly needs not to be loaded explicitly but the shape types must still be registered with the project. And that is what you should do now, since WebVisits does not require dynamic libraries.
1. | Add the line |
project1.AddLibrary(typeof(Ellipse).Assembly, false); |
right before the project creation.
2. | Run the application and see how everything works again. |
A new project requires a least one library to offer shapes.
You may wonder how the libraries were loaded in the previous step. How did NShape know, after all, which library it had to load? For the answer look at this snippet from the "Circles.nspj" project file:
<?xml version="1.0" encoding="utf-8"?> |
As you can see, when a project is saved, the list of required libraries is part of the stored information. So, whenever NShape opens a project it tries to find the required libraries (in the directories defined by LibrarySearchPaths) and loads them (if AutoLoadLibraries is allowed).