次の方法で共有


CreateFromXaml

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates XAML content dynamically.

retval = silverlightObject.content.CreateFromXaml(xamlContent)
-or-
retval = silverlightObject.content.CreateFromXaml(xamlContent, createNameScope)

Arguments

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 it is not specified.

Return Value

Type: object

An object reference if the XAML content was successfully created; otherwise, null.

Managed Equivalent

XamlReader.Load is the nearest equivalent. Note that XamlReader.Load has different namescope behavior. See Using XamlReader.Load.

Remarks

This method is available on the content subobject of a Silverlight plug-in instance.

The input for CreateFromXaml must meet certain requirements. See XAML and XAML-Related Concepts in the JavaScript API for Silverlight.

Caution noteCaution:

To guard your Silverlight-based application against security attacks, you should never 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 security threat. Additionally, XAML potentially 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 CreateFromXaml is from a source that your Silverlight-based application considers trusted.

CreateFromXaml, Name properties assigned in the content provided for CreateFromXaml, and run-time FindName calls all exist in a relationship that is governed by the concepts of an object tree and one or more XAML namescopes within that object tree. For more information, see XAML and XAML-Related Concepts in the JavaScript API for Silverlight.

Example

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="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);

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.

Applies To

Silverlight Plug-in