Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Lesson 2: Using Resources > Retrieving Resources in Code

Retrieving Resources in Code

You can access resources in code. The FindResource method enables you to obtain a reference to a resource by using the Key value. To use the FindResource method, you must call it from an element reference that has access to that resource. The following code example demonstrates how to obtain a reference to a resource with a Key value of myBrush through a Button element that has access to that resource:

Sample of Visual Basic Code

Dim aBrush As SolidColorBrush
aBrush = CType(Button1.FindResource("myBrush"), SolidColorBrush)

Sample of C# Code

SolidColorBrush aBrush;
aBrush = (SolidColorBrush)Button1.FindResource("myBrush");

The FindResource method throws an exception if the named resource cannot be found. To avoid possible exceptions, you can use the TryFindResource method instead.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint