Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FrameworkContentElement.FindResource Method

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

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
'Declaration
Public Function FindResource ( _
	resourceKey As Object _
) As Object

Parameters

resourceKey
Type: System.Object

Key identifier of the resource to be found.

Return Value

Type: System.Object
The found resource, or Nothing if no matching resource was found (but will also throw an exception if Nothing).
ExceptionCondition
ResourceReferenceKeyNotFoundException

The requested resource key was not found.

ArgumentNullException

resourceKey is Nothing.

Important noteImportant

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

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.

	  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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.