Creates XAML content dynamically.
| XAML | Cannot use methods in XAML. |
| Scripting | retval = silverlightObject.content.CreateFromXaml(xamlContent[, createNameScope])
|
Parameters
| xamlContent | string The XAML content to add to the existing Silverlight object hierarchy. |
| createNameScope | Boolean Determines whether to create x:Name references in XAML content
that do not conflict with other named elements. The createNameScope parameter is optional, and its value defaults to false if not specified. |
Return Value
object
An object reference if the XAML content was successfully created; otherwise, returns null.
Examples
The following JavaScript example shows how to add a TextBlock child object to an existing Canvas object:
| JavaScript |
// 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 XML namespace reference as part of the
XAML content. For example, in order for the previous XAML fragment example to
use the x:Name attribute value, it would need to be rewritten as:
| JavaScript |
// 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);
|
If you use an x:Name attribute value that is
already identifies an object in the Silverlight object hierarchy and createNameScope is false, an error is generated when you invoke the Add method for the XAML fragment. Set createNameScope to true, if you want to create multiple instances of elements without naming conflicts.
Applies To
Silverlight Plug-in
See Also
Using the CreateFromXaml Method
Silverlight XAML Syntax