c# - 我应该如何模板化这个 svg 片段?

标签 c# html css templates svg

我需要制作一个模板,我将通过查找特殊标签并替换值来为下面的坐标插入新值。

我已经给出了一个 SVG,下面是我需要更改的一小段内容,以便我可以模板化/添加占位符,因为下面的值是动态的。

<g id="Layer_1">
    <g id="shape1">
        <polygon class="poly1" points="6.7,105.4 -19.2,190.5 -20.4,123.5 1.6,143.6 "/>
    </g>
    <polygon id="shape2" class="poly1" points="-20.9,300.3 -37.7,103.3 -10.9,204.3 -0.1,233.6"/>
    <circle id="circleSolid" class="poly2" cx="6.7" cy="306.1" r="10.3"/>
    <g id="circleDashed">
        <circle class="poly3" cx="2.6" cy="304.7" r="10.3"/>
    </g>
    <text transform="matrix(1 0 0 1 -40.7301 191.3002)" class="poly1 poly3 poly4">Poly1</text>
    <text transform="matrix(1 0 0 1 -40.7302 200.3002)" class="poly1 poly3 poly4">Poly2</text>
</g>
<g id="Layer_7">
    <line id="newShape1" class="poly5" x1="-30.7" y1="170.7" x2="12.2" y2="171.7"/>
    <line id="newShape2" class="poly5" x1="-11.5" y1="160.7" x2="0.1" y2="200.3"/>
</g>>

我想知道我不能只在下面的 html 标签中添加 [x1] 标签来进行模板化。我应该只使用这些类来插入新的动态值吗?我应该如何对 SVG 绘图进行模板化?

我将在 C# 中动态插入新值。

最佳答案

您必须使用 C# 函数 String.Format(),它将接收您的字符串并将字符串中的任何变量替换为输出中的实际文本。

例如:

string name = "Scott";
string output = String.Format("Hello {0}", name); ///outputs Hello Scott

所以如果你想在 SVG 中生成你的圆形元素,你会有这样的代码:

string cx = '6.7';
string cy = '306.1';
string r = 10.3';
string result = String.Format("<circle id="circleSolid" class="poly2" cx="{0}" cy="{1}" r="{2}"/>", cx, cy, r);

输出如下:

<circle id="circleSolid" class="poly2" cx="6.7" cy="306.1" r="10.3"/>

关于c# - 我应该如何模板化这个 svg 片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33279180/

相关文章:

c# - GNUPG:在删除公钥时抑制消息

c# - 没有 WaitForExit() 的 using 语句中的进程会发生什么情况?

javascript - 使用 JavaScript 启用/禁用按钮

html - 在 CSS 中生成对 Angular 线(条纹)

javascript - 反向滚动和粘性元素

javascript - 正则表达式从矩阵字符串中获取单位

javascript - vue-fullcalendar - css 类和换行符不起作用

javascript - Angular Material 日期选择器大小

c# - 如何装饰 MediatR 处理程序

c# - 在 C# 中导航集合的通用列表