Επεξεργασία

Κοινή χρήση μέσω


FrameworkContentElement.TryFindResource(Object) Method

Definition

Searches for a resource with the specified key, and returns that resource if found.

public:
 System::Object ^ TryFindResource(System::Object ^ resourceKey);
public object TryFindResource (object resourceKey);
member this.TryFindResource : obj -> obj
Public Function TryFindResource (resourceKey As Object) As Object

Parameters

resourceKey
Object

Key identifier of the resource to be found.

Returns

The found resource. If no resource was found, null is returned.

Examples

The following example finds a resource as defined in markup and applies it to a certain property of an element in response to a routed event.

void SetBGByResourceTry(object sender, RoutedEventArgs e)
{
    Block b = sender as Block;
    b.Background = (Brush)this.TryFindResource("RainbowBrush");
}
Private Sub SetBGByResourceTry(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim b As Block = TryCast(sender, Block)
    b.Background = CType(Me.TryFindResource("RainbowBrush"), Brush)
End Sub

Remarks

If the resource is not found on the calling element, the parent tree is searched using the logical tree, in the same way that the tree would be searched if a dynamic resource reference were requested by key at run-time.

Typically you would immediately cast the return value to the type of the property that you were attempting to set with the returned resource value.

The FindResource method has similar behavior, except that it will throw an exception in the case of not finding a resource with the provided key.

Applies to

See also