有没有办法将Silverlight文本渲染到路径或图形对象在运行时?我知道这可以使用设计工具完成,但我希望能够即时执行此操作。
我见过calls a webservice which uses WPF constructs to convert a WPF FormattedText object to a PathGeometry的示例,但Silverlight中没有这些对象。
我很确定Silverlight不支持这个,但认为值得一提。
答案 0 :(得分:1)
如您所料,您必须在服务器端将文本转换为Pathlight,Silverlight支持 。 你想要实现什么目标?
答案 1 :(得分:0)
以下是动态创建SilverLight路径Geometry的代码。 或者从字符串创建SilverLight路径。 只需将下面的方法粘贴到新类或现有类中即可使用。 你可以在方法的底部找到一个示例代码来使用这个函数。
/// Method tested with SilverLight 2.0
// this method will generates path with the data string
public PathGeometry getPathGeometry(string data)
{
PathGeometry pg = new PathGeometry();
PathSegmentCollection psc = new PathSegmentCollection();
PathFigure pf = new PathFigure();
PathFigureCollection pfc = new PathFigureCollection();
data= data.Replace("M "," M").Replace("C "," C").Replace("L "," L");
string[] str = data.Split(' ');
for (int i = 0; i < str.Length; i++)
{
if (str[i].StartsWith("C") || str[i].StartsWith("c"))
{
string[] item = str[i].Split(',');
string[] item1 = str[i + 1].Split(',');
string[] item2 = str[i + 2].Split(',');
BezierSegment bs = new BezierSegment();
bs.Point1 = new Point(double.Parse(item[0].Substring(1)), double.Parse(item[1]));
bs.Point2 = new Point(double.Parse(item1[0]), double.Parse(item1[1]));
bs.Point3 = new Point(double.Parse(item2[0]), double.Parse(item2[1]));
i += 2;
psc.Add(bs);
}
else if (str[i].StartsWith("L") || str[i].StartsWith("l"))
{
string[] item = str[i].Split(',');
LineSegment ls = new LineSegment();
ls.Point = new Point(double.Parse(item[0].Substring(1)), double.Parse(item[1]));
psc.Add(ls);
}
else if (str[i].StartsWith("M") || str[i].StartsWith("m"))
{
string[] item = str[i].Split(',');
pf.StartPoint = new Point(double.Parse(item[0].Substring(1)), double.Parse(item[1]));
}
else if (str[i].StartsWith("z") || str[i].StartsWith("Z"))
{
pf.IsClosed = true;
}
}
pf.Segments = psc;
pfc.Add(pf);
pg.Figures = pfc;
return pg;
}
///// End of Method
调用方法的示例代码
Path path = new Path();
string str = " F1 M 933.291,430.505C 924.367,415.673 923.007,387.822 922.503,370.604C 921.343,331.31 944.994,317.76 975.999,296.994L 949.334,299.957C 938.729,302.545 930.572,309.925 920.255,313.368C 901.85,319.521 886.504,313.062 870.896,303.53C 850.12,290.842 831.457,270.65 815.107,251.462C 806.279,241.101 798.257,221.598 781.986,226.017C 767.327,229.99 760.199,246.869 743.058,244.012C 737.559,227.262 741.368,204.78 739.591,187.029C 738.108,172.136 733.986,158.933 733.996,143.736C 734.003,128.417 734.091,113.088 733.996,97.7689C 733.909,83.5475 730.302,82.6582 716.114,86.0475C 687.558,92.8796 663.68,115.232 634.418,119.337C 622.391,121.028 598.323,121.184 603.745,103.642C 603.745,103.642 547.667,116.478 522.623,101.969L 397.73,43.1915C 374.54,33.5875 352.799,21.5236 330.186,10.7568C 315.067,3.55951 298.84,3.50623 282.684,6.54358C 268.628,9.18353 252.14,8.36884 238.73,13.0222C 227.932,16.7648 225.711,27.0569 220.839,35.6369C 204.622,64.1582 184.474,89.9609 163.49,115.642C 143.3,140.356 124.747,161.949 100.268,182.977C 76.4618,203.437 58.0045,230.722 39.6698,256.062C 27.9845,272.228 10.5298,295.73 5.62447,315.546C 1.21381,333.368 7.65381,345.95 16.7778,360.225C 30.9738,382.42 52.4365,394.917 74.4578,408.658C 108.356,429.826 144.964,432.43 182.619,439.202C 194.226,441.284 201.93,444.466 212.456,450.234C 228.9,459.261 246.18,466.181 262.031,476.002C 277.378,485.518 288.175,498.328 306.771,498.502C 331.423,498.729 342.159,498.364 359.554,517.221C 368.632,527.06 372.859,537.585 380.38,548.114C 395.159,568.82 409.076,590.689 426.295,609.442C 440.326,624.728 467.967,633.601 487.652,636.902C 505.622,639.908 521.979,632.736 535.859,620.806C 545.402,612.606 552.478,602.246 557.978,591.161C 561.915,583.213 564.966,568.085 572.399,564.296C 578.046,561.41 595.117,563.91 601.338,564.312C 612.171,565.009 621.722,568.994 632.552,569.976C 651.071,571.65 654.679,567.992 668.187,558.989C 681.275,550.254 697.746,547.268 711.451,538.109C 733.726,523.208 751.861,501.273 773.035,484.254C 795.099,466.53 815.65,437.337 845.207,434.924C 871.813,432.754 933.291,430.505 933.291,430.505 Z";
path.Data = getPathGeometry(str);