FrameworkElement.FindResource(Object) FrameworkElement.FindResource(Object) FrameworkElement.FindResource(Object) FrameworkElement.FindResource(Object) Method

定義

以指定的索引鍵搜尋資源,如果找不到要求的資源時會擲回例外狀況。Searches for a resource with the specified key, and throws 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

參數

resourceKey
Object Object Object Object

所要求資源的索引鍵識別項。The key identifier for the requested resource.

傳回

要求的資源。The requested resource. 如果找不到含有所提供索引鍵的資源,便會擲回例外狀況。If no resource with the provided key was found, an exception is thrown. 在擲回例外狀況時,可能也會傳回 UnsetValue 值。An UnsetValue value might also be returned in the exception case.

例外狀況

找不到 resourceKeyUnhandledException 事件的事件處理常式也不存在。resourceKey was not found and an event handler does not exist for the UnhandledException event.

-或--or- 找不到 resourceKey,而且 Handled 屬性是 UnhandledException 事件中的 falseresourceKey was not found and the Handled property is false in the UnhandledException event.

範例

下列範例會取得已命名的資源, 並將它轉換成適當的類型, 以填滿屬性。The following example obtains a named resource and casts it to an appropriate type to fill a property.

void SetBGByResource(object sender, RoutedEventArgs e)
{
  Button b = sender as Button;
  b.Background = (Brush)this.FindResource("RainbowBrush");
}
Private Sub SetBGByResource(ByVal sender As Object, ByVal e As RoutedEventArgs)
  Dim b As Button = TryCast(sender, Button)
  b.Background = CType(Me.FindResource("RainbowBrush"), Brush)
End Sub

備註

重要

如果您為找不到的索引鍵呼叫這個方法, 則會擲回例外狀況。If you call this method for a key that cannot be found, an exception is thrown. 如果您不想要處理因呼叫FindResource而產生的例外狀況, 請改為呼叫。 TryFindResourceIf you do not want to handle exceptions that result from calling FindResource, call TryFindResource instead. TryFindResourcenull當找不到要求的資源時傳回, 而且不會擲回例外狀況。TryFindResource returns null when a requested resource cannot be found, and does not throw an exception.

如果在呼叫的元素上找不到資源, 則會搜尋邏輯樹狀結構中的父項目, 然後再進行應用程式、主題和最後的系統資源。If the resource is not found on the calling element, the parent element in the logical tree is searched next, then the application, then themes, and finally system resources. 此查閱方法與在標記中的動態資源參考要求資源時, 如何搜尋樹狀結構的方式相同。This lookup methodology is identical to how the tree is searched if a resource were requested by a dynamic resource reference in markup. 如需有關資源查閱的詳細資訊, 請參閱XAML 資源For more information about resource lookup, see XAML Resources.

一般來說, 您會立即將FindResource傳回值轉換為您使用所傳回資源值設定的屬性類型。Typically, you immediately cast a FindResource return value to the type of the property that you setting with the returned resource value.

資源金鑰不一定是字串。Resource keys are not necessarily strings. 比方說, 主題層級控制項的樣式會刻意放在控制項的Type中, 而控制項的應用程式或頁面樣式通常會使用相同的金鑰慣例。For instance, styles for controls at the theme level are deliberately keyed to the Type of the control, and application or page styles for controls typically use this same key convention. 如需詳細資訊, 請參閱設定樣式和範本化XAML 資源For details, see Styling and Templating or XAML Resources.

適用於

另請參閱