【问题标题】:Polyline only draws 2 lines & Point.Parse issue折线仅绘制 2 条线和 Point.Parse 问题
【发布时间】:2018-11-13 08:10:52
【问题描述】:

两个小问题,首先为什么我的Polyline 只连接字符串 W、X 和 Y。
其次是可以为多个点解析一个字符串,即:将所有数字存储在字符串 W 中,然后为所有点存储Point.Parse(W)

private void Button_Click(object sender, RoutedEventArgs e)
{
    string W = "0,0";
    string X = "99,99";
    string Y = " 99, 300";
    string Z = "600, 300";
    Point[] points = { Point.Parse(W), Point.Parse(X), Point.Parse(Y), Point.Parse (Z)};
    DrawLine(points);
}

private void DrawLine(Point[] points)
{
    Polyline line = new Polyline();
    PointCollection collection = new PointCollection();
    foreach (Point p in points)
    {
        collection.Add(p);
    }
    line.Points = collection;
    line.Stroke = new SolidColorBrush(Colors.Black);
    line.StrokeThickness =3;
    myGrid.Children.Add(line);
}

【问题讨论】:

  • 至于第二个问题:您可以为您的 Point[] 对象选择一个(反)序列化方法。
  • 那会是什么样子,我找不到任何适合我情况的好例子。 @PepitoSh
  • 任何可以处理对象数组并使用文本作为媒体的序列化方法:xml、json...

标签: c# parsing point polyline


【解决方案1】:

Polyline 连接以其Points 属性指定的顶点,以PointCollection 的形式表示。

您当前的 Points 集合定义了 4 个顶点,这将生成 3 条连接线:

Point(0, 0)Point(99, 99)的1行
1 行从 Point(99, 99)Point(99, 300)
Point(99, 300)Point(600, 300) 的 1 行

类似这样的:

\
 \
  \
   |
   |
   |____________

如果您没有看到这种结果,您的Grid 可能没有足够的空间来容纳所有绘图,然后这些绘图将被截断。

PointCollection.Parse() 方法允许您指定一个字符串,该字符串包含由逗号分隔的 Points 集合或由空格分隔的 Point 引用对。
这些都是有效的:

string points = "0,0,99,99,99,300,600,300";

string points = "0,0 99,99 99,300 600,300";

然后,您可以拥有一个包含所有 Points 引用的字符串。
您的代码可能会这样修改:

using System.Windows.Media;
using System.Windows.Shapes;

string points = "0,0,99,99,99,300,600,300";
PointCollection collection = PointCollection.Parse(points);
DrawLine(collection);


private void DrawLine(PointCollection points)
{
    Polyline line = new Polyline();
    line.Points = points;
    line.Stroke = new SolidColorBrush(Colors.Black);
    line.StrokeThickness = 3;
    myGrid.Children.Add(line);
}

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多