Bearbeiten

Freigeben über


FrameworkContentElement.FindResource(Object) Method

Definition

Searches for a resource with the specified key, and will throw an exception if the requested resource is not found.

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

Parameters

resourceKey
Object

Key identifier of the resource to be found.

Returns

The found resource, or null if no matching resource was found (but will also throw an exception if null).

Exceptions

The requested resource key was not found.

resourceKey is null.

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 SetBGByResource(object sender, RoutedEventArgs e)
{
    Block b = sender as Block;
    b.Background = (Brush)this.FindResource("RainbowBrush");
}
Private Sub SetBGByResource(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim b As Block = TryCast(sender, Block)
    b.Background = CType(Me.FindResource("RainbowBrush"), Brush)
End Sub

Remarks

Important

If you call this method for a key that cannot be found, an exception is thrown. If you do not want to handle exceptions for this case, you should instead call TryFindResource. TryFindResource returns null when no resource is found, and does not throw an exception.

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 resource 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.

Applies to