【发布时间】:2021-12-21 12:27:59
【问题描述】:
我正在尝试在 Blazor 中进行一些绘图操作,并希望根据用户选择绘制一些对象。因此,我试图将一个长字符串参数传递给 SVG 以绘制所需的元素。
对象类和方法
public class SupportRoller
{
public Point2D P1 { get; set; }
public Point2D P2 { get; set; }
public Point2D P3 { get; set; }
public Point2D L1 { get; set; }
public Point2D L2 { get; set; }
public Point2D C1 { get; set; }
public Point2D C2 { get; set; }
public static List<SupportRoller> SupportCoordinates(Point2D pinPoint, double scale)
{
var point1 = new Point2D(pinPoint.X * scale, pinPoint.Y * scale);
var point2 = new Point2D((pinPoint.X + 2.08) * scale, (pinPoint.Y + 4) * scale);
var point3 = new Point2D((pinPoint.X - 2.08) * scale, (pinPoint.Y + 4) * scale);
var line1 = new Point2D((pinPoint.X + 2.08) * scale, (pinPoint.Y + 5) * scale);
var line2 = new Point2D((pinPoint.X - 2.08) * scale, (pinPoint.Y + 5) * scale);
var circle1 = new Point2D((((line2.X - line1.X) / 3) + line2.X) * scale, (pinPoint.Y + 7) * scale);
var circle2 = new Point2D((((line2.X - line1.X) / 3) + circle1.X) * scale, (pinPoint.Y + 7) * scale);
var result = new List<SupportRoller>()
{
new SupportRoller() {P1 = point1, P2 = point2, P3 = point3, L1 = line1, L2 = line2, C1 = circle1, C2 = circle2}
};
return result;
}
public static string CoordinatesToString(List<SupportRoller> pointList)
{
string s1x = pointList[0].P1.X.ToString();
string s1y = pointList[0].P1.Y.ToString();
string s2x = pointList[0].P2.X.ToString();
string s2y = pointList[0].P2.Y.ToString();
string s3x = pointList[0].P3.X.ToString();
string s3y = pointList[0].P3.Y.ToString();
//string result1 = "<polygon points=\'" + s1x + "," + s1y + " " + s2x + "," + s2y + " " + s3x + "," + s3y + "\' />";
//Console.WriteLine(result1);
string l1x = pointList[0].L1.X.ToString();
string l1y = pointList[0].L1.Y.ToString();
string l2x = pointList[0].L2.X.ToString();
string l2y = pointList[0].L2.Y.ToString();
//string result2 = "<line x1 = \'" + l1x + "\' y1=\"" + l1y + "\' x2=\"" + l2x + "\' y2=\"" + l2y + "\' stroke=\'black\' stroke-width=\'0.2\' />";
string c1x = pointList[0].C1.X.ToString();
string c1y = pointList[0].C1.Y.ToString();
string c2x = pointList[0].C2.X.ToString();
string c2y = pointList[0].C2.Y.ToString();
var radius = 4;
string result = "<polygon points=\'" + s1x + "," + s1y + " " + s2x + "," + s2y + " " + s3x + "," + s3y + "\' /><line x1 = \'" + l1x + "\' y1=\'" + l1y + "\' x2=\'" + l2x + "\' y2=\'" + l2y + "\' stroke=\'black\' stroke-width=\'0.2\' /><circle cx=\'" + c1x + "\' cy=\'" + s1y + "\' r=\'" + radius + "\' /> <circle cx=\'" + s2x + "\' cy=\'" + s2y + "\' r=\'" + radius + "\' />";
Console.WriteLine(result);
//var resultMod1 = result.Remove('\"');
//var resultMod1 = result.Replace("\"", "");
Console.WriteLine(result);
return result;
}
}
我想在 SVG 中显示字符串,但是没有显示任何内容,因为字符串是通过 start 和 end "" 传递的,因此被忽略了。
下面显示在html中调用变量的地方:
<g>@mainDwg.EndSupport</g>
<g>"<polygon points='190,50 192.08,54 187.92,54' />
<line x1 = '192.08' y1='55' x2='187.92' y2='55' stroke='black' stroke-width='0.2' />
<circle cx='186.5333333333333' cy='50' r='4' /><circle cx='192.08' cy='54' r='4' />"</g>
如下调用它
mainDwg.EndSupport = SupportRoller.CoordinatesToString(endSupCoordRoller).Trim();
我尝试用多个函数修剪字符串但没有成功。
yourString = yourString.Replace('"', ' ').Trim();
yourString= yourString.Trim('"');
yourString=yourString.Replace("\"", "");
yourString = yourString.Replace("\"", string.Empty).Trim();
有人可以帮忙吗?而不是“你的字符串”,我只需要传递你的字符串。谢谢。
【问题讨论】:
-
发布的代码似乎不对声称的输出负责。
string result = "<polygon points=\' ...."; Console.WriteLine(result);不可能输出以<g>"<polygon points='开头的字符串 -
我敢打赌,在您程序的其他地方,您在发布代码之前输出“
”,在发布代码之后输出“ ”。 -
对不起,输出以“
...”开头,没有 变量被插入的地方。 @mainDwg.EndSupport -
那么
<g>@mainDwg.EndSupport</g>是剃刀标记吗,mainDwg.EndSupport真的是像<polygon poi... r='4' />这样的字符串,而您声称剃刀渲染在它周围添加了""?有 MCRE 吗? -
是的,是的,是的,什么是 MCRE?