The following JavaScript example shows how to add a TextBlock child object to an existing Canvas object.
// Create the MouseLeftButtonUp event handler for the root Canvas object.
function onMouseLeftButtonUp(sender, eventArgs)
{
// Retrieve a reference to the plug-in.
var plugin = sender.getHost();
// Define a XAML fragment and create it.
var xamlFragment = '<TextBlock Canvas.Top="200" Text="Click for more info..." />';
textBlock = plugin.content.createFromXaml(xamlFragment, false);
// Add the XAML fragment as a child of the root Canvas object.
sender.children.add(textBlock);
}
If you want to use the x:Name attribute value as part of your XAML fragment, you will need to provide the XAML namespace reference as part of the XAML content. For example, for the previous XAML fragment example to use the x:Name attribute value, you would need to rewrite the example as follows:
// Define a XAML fragment and create it.
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 = plugin.content.createFromXaml(xamlFragment, false);
In the XAML fragment, if you use a Name or x:Name attribute value that already identifies an object in the Silverlight object tree, and createNameScope is false, an error is raised when you invoke the Add method by using the generated object. Set createNameScope to true if you want to create multiple instances of elements without causing naming conflicts.