我有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"/>");
显然使用适当的转义符来确保字符串正确解析。
我记得这是可能的,但我记不清具体细节了。
答案 0 :(得分:2)
刚刚想通了,使用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"/>);
(显然还需要为引号添加转义字符)