createFromXaml允许你动态的创建xaml。
下面的例子动态的创建一个圆.
![](https://i-blog.csdnimg.cn/blog_migrate/fc80dbdd31f39d833aa4c05ae8077542.jpeg)
function drawEllipse(sender,eventArgs)
{
var control = sender.getHost();
var xamlFragment = '<Ellipse Canvas.Top="200" Width="100" Height="100" Fill="Teal" />';
var ellipse = control.content.createFromXaml(xamlFragment);
sender.findName("root").children.add(ellipse);
}
{
var control = sender.getHost();
var xamlFragment = '<Ellipse Canvas.Top="200" Width="100" Height="100" Fill="Teal" />';
var ellipse = control.content.createFromXaml(xamlFragment);
sender.findName("root").children.add(ellipse);
}
注意:如果你在添加的元素中使用了x:Name,请务必在这个元素中添加xml名字空间。
var xamlFragment = '<TextBlock xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ';
xamlFragment += 'x:Name="myCanvas" Canvas.Top="200" Text="Click for more info" />';
textBlock = control.content.createFromXaml(xamlFragment);
xamlFragment += 'x:Name="myCanvas" Canvas.Top="200" Text="Click for more info" />';
textBlock = control.content.createFromXaml(xamlFragment);