共用方式為


MRT 到 MRT 核心移轉

本主題包含從 UWP 的資源管理系統 (也稱為 MRT) 移轉至 Windows 應用程式 SDK MRT 核心的指引。

MRT Core 是精簡版的 MRT。 有關更多資訊,請參閱使用 MRT Core 管理資源

API 和/或功能差異摘要

為了方便移轉,MRT 核心 API 與 MRT API 非常類似。 如需 API 參考檔,請參閱 Microsoft.Windows.ApplicationModel.Resources 命名空間

注意

並非所有 MRT API 都存在於 MRT Core 中。 但包含 MRT 基本功能所需的所有 API。

變更命名空間

在UWP中,MRT API位於 Windows.ApplicationModel.Resources.Core 命名空間中。 在 Windows 應用程式 SDK 中,MRT 核心 API 位於 Microsoft.Windows.ApplicationModel.Resources 命名空間中。 因此,您必須在原始碼中變更該命名空間名稱 (在開頭新增 Microsoft.,並在結尾移除 .Core)。

注意

在 Windows 應用程式 SDK 1.0 Preview 1 和更新版本中,MRT Core API 位於 Microsoft.Windows.ApplicationModel.Resources 命名空間中。 在稍早的版本中,它們位於 Microsoft.ApplicationModel.Resources 命名空間中。

ResourceManager 類別

如果您在 UWP 應用程式中使用 Windows.ApplicationModel.Resources.Core.ResourceManager.Current 屬性,則適用本節。

// In a UWP app
using Windows.ApplicationModel.Resources.Core;
...
var currentResourceManager = ResourceManager.Current;
// In a UWP app
#include <winrt/Windows.ApplicationModel.Resources.Core.h>
using namespace winrt::Windows::ApplicationModel::Resources::Core;
...
auto currentResourceManager{ ResourceManager::Current() };

相反地,在您的 Windows 應用程式 SDK 應用程式中,建立新的 Microsoft.Windows.ApplicationModel.Resources.ResourceManager

// In a Windows App SDK app
using Microsoft.Windows.ApplicationModel.Resources;
...
var currentResourceManager = new ResourceManager();
// In a Windows App SDK app
#include <winrt/Microsoft.Windows.ApplicationModel.Resources.h>
using namespace winrt::Microsoft::Windows::ApplicationModel::Resources;
...
ResourceManager currentResourceManager;

ResourceContext.GetForCurrentView 和 ResourceContext.GetForViewIndependentUse

UWP 的 MRT ResourceContext 類別會區分目前檢視的 ResourceContext,另一個則用於檢視獨立使用。

針對 Windows 應用程式 SDK 的 MRT Core ResourceContext 類別,您的應用程式必須判斷正確的內容 (資源限定符值),以及目前檢視和檢視獨立使用的概念不再適用。

資源限定符值

在 UWP 的 MRT 中,會針對應用程式決定資源內容限定符值。 在 MRT Core 中,只會填入語言值。 您的應用程式必須判斷自己的其他值。 以下是範例,假設您的 XAML 檢視包含名為 layoutRoot 的專案。

// In a Windows App SDK app
using Microsoft.Windows.ApplicationModel.Resources;
...
var currentResourceManager = new ResourceManager();
var resourceContext = currentResourceManager.CreateResourceContext();
int scaleFactor = Convert.ToInt32(layoutRoot.XamlRoot.RasterizationScale * 100);
resourceContext.QualifierValues[KnownResourceQualifierName.Scale] = scaleFactor.ToString();
string s = resourceContext.QualifierValues[KnownResourceQualifierName.Scale];
// In a Windows App SDK app
#include <winrt/Microsoft.Windows.ApplicationModel.Resources.h>
using namespace winrt::Microsoft::Windows::ApplicationModel::Resources;
...
ResourceManager currentResourceManager;
auto resourceContext{ currentResourceManager.CreateResourceContext() };
auto scaleFactor{ layoutRoot().XamlRoot().RasterizationScale() * 100 };
resourceContext.QualifierValues().Insert(L"Scale", std::to_wstring((int)scaleFactor));
auto s{ resourceContext.QualifierValues().Lookup(L"Scale") };

資源限定符值變更

UWP 的 MRT 提供 ResourceQualifierObservableMap.MapChanged 事件。 如果 UWP 應用程式正在處理該事件,以便接聽限定符值變更,則本節適用。

MRT Core 不會針對環境變更提供任何通知機制。 因此,如果您想要根據環境變更更新資源,您的 Windows 應用程式 SDK 應用程式必須自行偵測這類變更。

MRT Core 範例應用程式

另請參閱使用 MRT Core 範例應用程式專案載入資源,其中示範如何使用 MRT Core API 介面。