CustomXamlResourceLoader 類別

定義

啟用將自訂資源字典載入為來源。 覆寫 GetResource 以提供 XAML 剖析器可在載入時間用來查閱必要資源的邏輯。

/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class CustomXamlResourceLoader
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class CustomXamlResourceLoader
Public Class CustomXamlResourceLoader
繼承
Object IInspectable CustomXamlResourceLoader
屬性

備註

CustomXamlResourceLoader 支援標記延伸使用方式 {CustomResource} 。 此標記延伸對Windows 執行階段 XAML 架構而言是唯一的, (它不存在於Windows Presentation Foundation (WPF) 和 Microsoft Silverlight.)

{CustomResource} 用法預設無法運作 (GetResource 的基底實作不完整)。 為了讓 {CustomResource} 參考有效,您必須執行下列每個步驟:

  1. 從 CustomXamlResourceLoader 衍生自訂類別並覆寫 GetResource 方法。 不要在實作中呼叫基底。
  2. 設定 CustomXamlResourceLoader.Current 使其參考初始化邏輯中的類別。 這必須在載入任何包含 {CustomResource} 延伸用法的頁面層級 XAML 之前完成。 其中一個設定 CustomXamlResourceLoader.Current 的位置,就是在 App.xaml 程式碼後置範本中為您產生的 Application 子類別建構函式。
  3. 現在您可以在應用程式載入為頁面的 XAML 中使用 {CustomResource} 延伸,或是從 XAML 資源字典內使用。

{CustomResource} 標記延伸 可從 XAML 使用,類似于 {StaticResource} 標記延伸,其會從各種 ResourceDictionary 位置執行內建資源查閱。 差異在於應用程式本身會藉由提供自訂資源載入器,實作 {CustomResource} 標記延伸 使用量的資源查閱邏輯。 若要實作自訂資源載入器,應用程式必須提供衍生自 CustomXamlResourceLoader 的類別,並覆寫 GetResource 方法。 應用程式必須將 static 屬性 CustomXamlResourceLoader.Current 設定為其自訂資源載入器類別的實例,以註冊其自訂資源載入器。 此註冊必須在 XAML UI 執行緒上完成,才能使用 {CustomResource} 標記延伸的任何 XAML 剖析。 如果應用程式使用多個 UI 執行緒,則必須在每個執行緒上完成個別的註冊。

CustomXamlResourceLoader 儲存其資源的方式可能完全與 ResourceDictionary 無關。 一般而言,除了必要的覆寫之外,CustomXamlResourceLoader 類別還會提供內部或公用方法來啟用儲存、索引鍵處理,以及使用基礎集合中的資源專案。

建構函式

CustomXamlResourceLoader()

初始化 CustomXamlResourceLoader 類別的新實例。

屬性

Current

傳回使用中的 CustomXamlResourceLoader 實例。

方法

GetResource(String, String, String, String)

在衍生類別中覆寫時,指定這個 CustomXamlResourceLoader的資源查閱邏輯。 給定資源識別碼和預期結果的一些類型資訊,會傳回要求的資源。

適用於

另請參閱