在代码隐藏中从 XAML 创建控件?
我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚发现,使用 XamlReader.Load 效果很好。
我必须对代码进行的一个小修改是添加命名空间,因此要转换它:
要通过 XamlReader.Load 运行,我这样做:(
显然还必须为引号添加转义字符)
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:
To run via XamlReader.Load I do:
(obviously would also have to add escape characters for quotation marks)