如何在WPF中通过点创建几何? [英] How to create a geometry by points in WPF?

查看:117
本文介绍了如何在WPF中通过点创建几何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:添加点以形成几何.
问题:该怎么做?
解决方法:我可以创建折线,然后通过RenderedGeometry将其转换为几何.
注意:必须使用不带XAML的C#代码完成.

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.

推荐答案

首先,此类问题与XAML无关.我的意思是,您在XAML中可以做的所有事情都可以在没有XAML的情况下完成.实际上,该构建完全消除了XAML(用于Windows,自定义控件等),它可以执行以下操作:将XAML转换为C#(在您所用的情况下)代码,并使用该代码构建程序集,就像在任何程序中一样其他代码.

所以,我的主要建议:如果您想知道如何在不使用XAML的情况下做某事,而只知道如何使用XAML,请实际编写所需的XAML并构建项目.然后找到自动生成的C#代码(在项目的"obj"子目录下),并查看其工作方式.

现在,您自己指出,如果您具有某个形状的实例,则可以通过RenderedGeometry"获得Geometry的实例.这实际上意味着您可以使用属性System.Windows.Shapes.Shape.RenderedGeometry:Shape类System.Windows.Shapes.Polyline,因此您也可以将此属性用于该类.我不明白这怎么可能是个问题.否则我不明白为什么您会认为这是解决方法".这可能是最简单的方法.也许您不希望有任何形状,因为它与您的解决方案无关.这样我就可以理解.

然后,第二种最简单的方法是使用类System.Windows.Media.PathGeometry从头开始创建Geometry并创建Geometry实例,该类是从System.Windows.Media.Geometry派生的类.如果仅需要一条折线,则仅使用一个PathFigure成员,例如,抛出构造函数
https://msdn.microsoft.com/en-us/library/ms558591 (v = vs.110).aspx [ https://msdn.microsoft.com/en-us/library/ms558590 (v = vs.110).aspx [ https://msdn.microsoft.com/en-us/library/ms602543%28v = vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/zh-cn/library/system.windows.media.pathsegment%28v = vs.110%29.aspx [ 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 [
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


这篇关于如何在WPF中通过点创建几何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆