[英]Create control from XAML in codebehind?
I have XAML defining something, say: 我有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"/>
Can I do something like this in codebehind: 我可以在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.. 我记得这是可能的,但我不记得具体细节..
Just figured it out, using XamlReader.Load does it nicely. 刚想通了,使用XamlReader.Load做得很好。
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: 要通过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"/>);
(obviously would also have to add escape characters for quotation marks) (显然还需要为引号添加转义字符)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.