我有一个XAML路径
<Path Width="30" Height="40" Stretch="Fill" Fill="Red" Data="F1 M 36.4167,19C 44.2867,19 50.6667,24.6711 50.6667,31.6667C 50.6667,32.7601 50.5108,33.8212 50.2177,34.8333L 36.4167,57L 22.6156,34.8333C 22.3225,33.8212 22.1667,32.7601 22.1667,31.6667C 22.1667,24.6711 28.5466,19 36.4167,19 Z M 36.4167,27.7083C 34.2305,27.7083 32.4583,29.4805 32.4583,31.6667C 32.4583,33.8528 34.2305,35.625 36.4167,35.625C 38.6028,35.625 40.375,33.8528 40.375,31.6667C 40.375,29.4805 38.6028,27.7083 36.4167,27.7083 Z "/>
如何将其转换为C#中的代码,例如:
public static Grid Icon()
{
Grid mygrid = new Grid();
string thedata = "F1 M 36.4167,19C 44.2867,19 50.6667,24.6711 50.6667,31.6667C 50.6667,32.7601 50.5108,33.8212 50.2177,34.8333L 36.4167,57L 22.6156,34.8333C 22.3225,33.8212 22.1667,32.7601 22.1667,31.6667C 22.1667,24.6711 28.5466,19 36.4167,19 Z M 36.4167,27.7083C 34.2305,27.7083 32.4583,29.4805 32.4583,31.6667C 32.4583,33.8528 34.2305,35.625 36.4167,35.625C 38.6028,35.625 40.375,33.8528 40.375,31.6667C 40.375,29.4805 38.6028,27.7083 36.4167,27.7083 Z";
Path path = new Path();
path.Height = 40;
path.Width = 30;
path.Fill = new SolidColorBrush(Colors.Red);
path.SetValue(System.Windows.Shapes.Path.DataProperty, thedata);
mygrid.Children.Add(path);
return mygrid;
}
上面的代码有问题。有什么建议吗?
答案 0 :(得分:1)
您遇到此问题是因为“thedata”是一个字符串。
要解决此问题,请使用Geometry类来解析“thedata”字符串:
public static Grid Icon()
{
Grid mygrid = new Grid();
string thedata = "F1 M 36.4167,19C 44.2867,19 50.6667,24.6711 50.6667,31.6667C 50.6667,32.7601 50.5108,33.8212 50.2177,34.8333L 36.4167,57L 22.6156,34.8333C 22.3225,33.8212 22.1667,32.7601 22.1667,31.6667C 22.1667,24.6711 28.5466,19 36.4167,19 Z M 36.4167,27.7083C 34.2305,27.7083 32.4583,29.4805 32.4583,31.6667C 32.4583,33.8528 34.2305,35.625 36.4167,35.625C 38.6028,35.625 40.375,33.8528 40.375,31.6667C 40.375,29.4805 38.6028,27.7083 36.4167,27.7083 Z";
Path path = new Path();
path.Height = 40;
path.Width = 30;
path.Fill = new SolidColorBrush(Colors.Red);
path.Data = Geometry.Parse(thedata);
mygrid.Children.Add(path);
return mygrid;
}