FrameworkElement.Resources 屬性

定義

取得本機定義的資源字典。 在 XAML 中,您可以透過 XAML 隱含集合語法,將資源專案建立為屬性元素的 frameworkElement.Resources 子物件專案。

ResourceDictionary Resources();

void Resources(ResourceDictionary value);
public ResourceDictionary Resources { get; set; }
var resourceDictionary = frameworkElement.resources;
frameworkElement.resources = resourceDictionary;
Public Property Resources As ResourceDictionary
<frameworkElement>
  <frameworkElement.Resources>
    oneOrMoreResourceElements
  </frameworkElement.Resources>
</frameworkElement>

屬性值

目前本機定義的資源字典,其中每個資源都可以由其索引鍵存取。

範例

此範例示範簡單 Resources 字典的 XAML 定義,其中包含一個專案 DataTemplate

<Grid.Resources>
  <DataTemplate x:Key="CBTemplate">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Image Grid.Column="0" Width="50" Height="50" 
        Source="{Binding Photo}" Stretch="Fill"/>
      <TextBlock Grid.Column="1" Text="{Binding Title}" 
        Margin="10" HorizontalAlignment="Left" FontSize="20"/>
    </Grid>
  </DataTemplate>
</Grid.Resources>
<GridView ItemTemplate="{StaticResource CBTemplate}" .../>

使用 XAML 資源定義和資源參考是使用 Resources 屬性的典型方式。 大部分情況下,XAML 只能處理常見的資源案例。 但您也可以使用 屬性來存取集合 API,因此,如果您的案例需要,請使用執行時間程式碼擷取資源。 此範例顯示內容的程式 Resources 代碼存取。 在此範例中, Resources 屬性參考是內嵌的,後面緊接著索引子用法,它會使用字串索引鍵 RainbowBrush 擷取ResourceDictionary專案。 請注意明確轉換; ResourceDictionary 中專案的傳回值一律為非類型物件。

void MainPage::SetBGByResource(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
  Button^ b = safe_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  b->Background = safe_cast<Windows::UI::Xaml::Media::Brush^>(this->Resources->Lookup("RainbowBrush"));
}
void SetBGByResource(object sender, RoutedEventArgs e)
{
  Button b = sender as Button;
  b.Background = (Brush)this.Resources["RainbowBrush"];
}
<Page.Resources>
...
  <LinearGradientBrush x:Key="RainbowBrush">
    <GradientStop Color="Red" Offset="0.05" />
    <GradientStop Color="Orange" Offset="0.23" />
    <GradientStop Color="Yellow" Offset="0.41" />
    <GradientStop Color="Green" Offset="0.59" />
    <GradientStop Color="Blue" Offset="0.77" />
    <GradientStop Color="Purple" Offset="0.95" />
 </LinearGradientBrush>
</Page.Resources>

備註

集合中 Resources 專案的主要目的是使用 {StaticResource} 標記延伸 參考 (或類似的 {ThemeResource} 標記延伸 參考) ,從 XAML 的其他部分參考它們。 如果您想要在執行時間存取 Resources 集合,您可以使用相關範本的 API 來查詢、新增或移除 ResourceDictionary中的專案。

如需詳細資訊和範例,請參閱 ResourceDictionary 和 XAML 資源參考

ResourceDictionary是索引鍵集合,如果您使用 Visual C++ 元件延伸模組 (C++/CX) 進行程式設計,則以IMap < K、V >範本為基礎,或者如果您是使用 C# 進行程式設計,則為 IDictionary < TKey,TValue >範本。 您在程式碼中用來處理字典及其專案的 API 會反映基礎範本,因此您用於應用程式的語言。

應用程式 也有 Resources 屬性,可用來儲存應該可從應用程式中多個頁面存取的資源。 自訂控制項的資源也可以儲存在範本化控制項的預設專案範本所建立的個別 XAML 檔案中。

您在 XAML Resources 集合中看到的專案不一定是執行時間可用的 XAML 定義資源的完整專案。 其他資源可在執行時間使用,因為 MergedDictionaries 屬性對 ResourceDictionary的影響。 值 MergedDictionaries 可以導入其他字典,例如系統定義的資源,例如預設 XAML 控制項範本中的資源。 執行時間主題特定資源也可以從類似的 ThemeDictionaries 屬性取得。 如果您在執行時間存取Resources集合,並使用索引子或Lookup方法來查詢特定索引鍵 Item ,則可以存取並擷取這些資源。 如需詳細資訊,請參閱 ResourceDictionary 與 XAML 資源參考。 此外, Application.Resources 可以提供可供應用程式中任何 XAML 參考使用的資源,進而擴充任何指定 FrameworkElement.Resources 字典中的資源。

適用於

另請參閱