Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
TryFindResource Method

Application.TryFindResource Method

Searches for the specified resource.

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

public Object TryFindResource (
	Object resourceKey
)
public Object TryFindResource (
	Object resourceKey
)
public function TryFindResource (
	resourceKey : Object
) : Object
You cannot use methods in XAML.

Parameters

resourceKey

The name of the resource to find.

Return Value

The requested resource object. If the requested resource is not found, a null reference is returned.

TryFindResource 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, TryFindResource 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 TryFindResource to acquire them, these types also expose resource key properties that are designed to be passed to TryFindResource eg IconWidthKey.

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

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

The following example shows how to use TryFindResource to acquire a resource.

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml"
    >
	<Application.Resources>
		<Image x:Key="ApplicationResource" Source="ApplicationResource.bmp" />
	</Application.Resources>
</Application>

void tryFindResourceButton_Click(object sender, RoutedEventArgs e) {
  object resource = Application.Current.TryFindResource("ApplicationResource");
  // If resource found, do something with it
  if (resource != null) {

...

  }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2015 Microsoft