在代码隐藏中从 XAML 创建控件?

发布于 2024-12-15 14:27:57 字数 577 浏览 5 评论 0原文

我有 XAML 定义某些内容,例如:

<Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>

我可以在代码隐藏中执行类似的操作吗:

Path myPath = Path.FromXAML("
    <Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>");

显然使用适当的转义字符来确保字符串正确解析。

我记得这是可能的,但我不记得具体细节了。

I have XAML defining something, say:

<Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>

Can I do something like this in codebehind:

Path myPath = Path.FromXAML("
    <Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>");

Obviously with appropriate escape characters to ensure the string parses properly.

I recall that this was possible but I can't remember the specifics..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

但可醉心 2024-12-22 14:27:57

刚刚发现,使用 XamlReader.Load 效果很好。

我必须对代码进行的一个小修改是添加命名空间,因此要转换它:

<Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>

要通过 XamlReader.Load 运行,我这样做:(

Path p = XamlReader.Load(<Path **xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"**  Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>);

显然还必须为引号添加转义字符)

Just figured it out, using XamlReader.Load does it nicely.

One small modification I have to make to the code is add the namespace, so to convert this:

<Path Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>

To run via XamlReader.Load I do:

Path p = XamlReader.Load(<Path **xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"**  Data="M16.25,11 L0.5,0.5 L32,0.5 z" Fill="#FFF4F4F5" Height="11" Margin="1.542,0.25,18.458,0" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Top"/>);

(obviously would also have to add escape characters for quotation marks)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文