Click here to Skip to main content
15,999,229 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Goal: To add points to form a geometry.
Question: How to do it?
Workaround: I can create Polyline and convert it into a geometry by RenderedGeometry.
Note: It must be done using C# code without XAML.
Posted
Updated 24-Jul-15 20:14pm
v3

1 solution

First of all such questions are irrelevant to XAML. I mean, all you can do in XAML you can do without XAML. In fact, XAML (in their use for windows, custom controls and the like) is fully eliminated by the build, which does the following: translate XAML in C# (in your case) code and builds the assembly with that code exactly as with any other code.

So, my first and main advice: if you want to know how to do something without XAML but only know how to do it with XAML, actually write XAML you want and build the project. Then find the auto-generated C# code (under your project's "obj" sub-directory) and see how it works.

Now, you pointed out yourself that if you have an instance of some shape, you can get an instance of Geometry "by RenderedGeometry". It really means that you can use the property System.Windows.Shapes.Shape.RenderedGeometry: https://msdn.microsoft.com/en-us/library/system.windows.shapes.shape.renderedgeometry%28v=vs.110%29.aspx[^].

As the word "Polyline" can be understood as the Shape class System.Windows.Shapes.Polyline, you can use this property for this class, too. I don't understand how it could be a problem. Or I don't understand why could you consider it a "work-around". It's probably the simplest way. Perhaps you don't want to have any shape because it's irrelevant to your solution; so I can understand it.

Then the second simplest way would be creating and instance of Geometry from scratch, using the class System.Windows.Media.PathGeometry, which is the class derived from System.Windows.Media.Geometry. If you need only one polyline, use only one PathFigure member, say, throw the constructors
https://msdn.microsoft.com/en-us/library/ms558591(v=vs.110).aspx[^],
or
https://msdn.microsoft.com/en-us/library/ms558590(v=vs.110).aspx[^].

You can just pass a one-element array with an instance of System.Windows.Media.PathFigure. In turn, such instance can be constructed from a collection of the elements of the class System.Windows.Media.PathSegment:
https://msdn.microsoft.com/en-us/library/ms602543%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.media.pathsegment%28v=vs.110%29.aspx[^].

In turn, for concrete kinds of type segments, you need to use the class derived from System.Windows.Media.PathSegment. For just a polyline, it can be a collection of segments of the type System.Windows.Media.LineSegment, or, alternatively, the whole polyline in one segment with the class System.Windows.Media.PolyLineSegment:
https://msdn.microsoft.com/en-us/library/system.windows.media.pathsegment%28v=vs.110%29.aspx#inheritanceContinued[^],
https://msdn.microsoft.com/en-us/library/system.windows.media.linesegment(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.media.polylinesegment(v=vs.110).aspx[^].

Assemble it together and you will get an instance of Geometry. Also note that you can use the class System.Windows.Media.CombinedGeometry to combine different types of geometry in one.

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900