Compartir a través de


FindName (Silverlight Plug-in)

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

Gets any named object in the Silverlight object hierarchy by referencing the object's x:Name or Name attribute value.

retval = silverlightObject.content.FindName(objectName)

Arguments

objectName

string

The name of the object to get.

Return Value

Type: object

A reference to the specified object if the object was successfully found; otherwise, null.

Managed Equivalent

FrameworkElement.FindName (or TextElement.FindName). XAML namescopes work a bit differently in the managed API; see XAML Namescopes.

Remarks

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

You can find any named object in the Silverlight object hierarchy by using the FindName method of the Silverlight plug-in and referencing the object's x:Name or Name attribute value. The object you are searching for can be any named object in the Silverlight object hierarchy. If the method is successful, it returns a reference to the object; otherwise, it returns null.

The behavior of FindName potentially depends on how or whether discrete XAML namescopes exist in your markup. For more information, see XAML and XAML-Related Concepts in the JavaScript API for Silverlight.

Because FindName relies on all XAML being parsed for the Name attributes, you should not attempt to call FindName until the XAML is loaded. You can assure this by calling FindName as part of a Loaded handler, or can use other user-driven event handlers that implicitly can occur only once the user interface is loaded.

Example

The following JavaScript example shows how use the FindName method.

function onLoaded(sender, eventArgs)
{
    // Retrieve a reference to the plug-in.
    var slPlugin = sender.getHost();

    // Retrieve a reference to the specified object.
    var object = slPlugin.content.findName("myTextBlock");

    // If the object reference is valid, display an alert dialog box.
    if (object != null)
    {
        alert(object.toString() + " found");
    }
}

Applies To

Silverlight Plug-in