CreateFromXaml Method
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 |
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.
Remarks
This method is available on the content sub-object of a Silverlight plug-in instance.
The CreateFromXaml method creates objects from XAML content dynamically, which you can then add to the existing Silverlight object hierarchy. You can create a single Silverlight object, such as a TextBlock, or an entire tree of Silverlight objects. For more information, see Using the CreateFromXaml Method.
The string you specify for xamlContent must specify well formed XML (notably, it must have a single root element). CreateFromXaml will fail and raise an error if the provided XML string is not well formed.
Note XAML content created by using the CreateFromXaml method is not rendered until it is added to an object using the Add method.
Note In order to guard your Silverlight application against security attacks, it is strongly recommended that you do not pass in untrusted XAML to the createFromXAML method. For example, untrusted XAML could contain a mock-up interface that mirrors a legitimate site, leading to a spoofing type security threat. Additionally, XAML contains references to script event handlers. Adding untrusted XAML to the live tree could result in unintended execution of script. Always validate that XAML passed in to createFrom XAML is from a source that your Silverlight application considers trusted.
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="https://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.