FindResource Method

Application::FindResource Method (Object^)

 

Searches for a user interface (UI) resource, such as a Style or Brush, with the specified key, and throws an exception if the requested resource is not found (see XAML Resources).

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

public:
Object^ FindResource(
	Object^ resourceKey
)

Parameters

resourceKey
Type: System::Object^

The name of the resource to find.

Return Value

Type: System::Object^

The requested resource object. If the requested resource is not found, a ResourceReferenceKeyNotFoundException is thrown.

Exception Condition
ResourceReferenceKeyNotFoundException

The resource cannot be found.

FindResource will first look in application-scope resources for the specified resource. Application-scope resources are managed by Application, and are exposed from the Resources property. If the specified resource is not found in the set of application-scope resources, FindResource then next searches the system resources. System resources are shell resources defined by the user, and include colors, fonts, and shell configurations. These are exposed from the SystemColors, SystemFonts, and SystemParameters types, respectively, as static properties. To use FindResource to acquire them, these types also expose resource key properties that are designed to be passed to FindResource; for example, IconWidthKey.

Because FindResource returns an object, you must cast the returned value to the appropriate type if the resource is found.

System_CAPS_importantImportant

If you call this method for a key that cannot be found, an exception is thrown. If you do not want to handle exceptions that result from calling FindResource, call TryFindResource instead; TryFindResource returns a null reference when a requested resource cannot be found, and does not throw an exception.

This method is thread safe and can be called from any thread.

The following example shows how to use FindResource to find a resource, and to handle ResourceReferenceKeyNotFoundException if the resource is not found.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 3.0
Return to top
Show:
© 2016 Microsoft