ResourceContext 클래스

정의

리소스 선택에 영향을 줄 수 있는 모든 요소(ResourceQualifiers)를 캡슐화합니다.

public ref class ResourceContext sealed
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class ResourceContext final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class ResourceContext final
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class ResourceContext
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class ResourceContext
function ResourceContext()
Public NotInheritable Class ResourceContext
상속
Object Platform::Object IInspectable ResourceContext
특성

Windows 요구 사항

디바이스 패밀리
Windows 10 (10.0.10240.0에서 도입되었습니다.)
API contract
Windows.Foundation.UniversalApiContract (v1.0에서 도입되었습니다.)

예제

이 예제는 애플리케이션 리소스 및 지역화 샘플의 시나리오 12를 기반으로 합니다. 전체 솔루션은 샘플을 참조하세요.

private async void Scenario12Button_Show_Click(object sender, RoutedEventArgs e)
{
    // Two coding patterns will be used:
    //   1. Get a ResourceContext on the UI thread using GetForCurrentView and pass 
    //      to the non-UI thread
    //   2. Get a ResourceContext on the non-UI thread using GetForViewIndependentUse
    //
    // Two analogous patterns could be used for ResourceLoader instead of ResourceContext.

    // pattern 1: get a ResourceContext for the UI thread
    ResourceContext defaultContextForUiThread = ResourceContext.GetForCurrentView();

    // pattern 2: we'll create a view-independent context in the non-UI worker thread

    // We need some things in order to display results in the UI (doing that
    // for purposes of this sample, to show that work was actually done in the
    // worker thread):
    List<string> uiDependentResourceList = new List<string>();
    List<string> uiIndependentResourceList = new List<string>();

    // use a worker thread for the heavy lifting so the UI isn't blocked
    await Windows.System.Threading.ThreadPool.RunAsync(
        (source) =>
        {
            ResourceMap stringResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");

            // pattern 1: the defaultContextForUiThread variable was created above and is visible here

            // pattern 2: get a view-independent ResourceContext
            ResourceContext defaultViewIndependentResourceContext = ResourceContext.GetForViewIndependentUse();

            // NOTE: The ResourceContext obtained using GetForViewIndependentUse() has no scale qualifier
            // value set. If this ResourceContext is used in its default state to retrieve a resource, that 
            // will work provided that the resource does not have any scale-qualified variants. But if the
            // resource has any scale-qualified variants, then that will fail at runtime.
            //
            // A scale qualifier value on this ResourceContext can be set programmatically. If that is done,
            // then the ResourceContext can be used to retrieve a resource that has scale-qualified variants.
            // But if the scale qualifier is reset (e.g., using the ResourceContext.Reset() method), then
            // it will return to the default state with no scale qualifier value set, and cannot be used
            // to retrieve any resource that has scale-qualified variants.

            // simulate processing a number of items
            // just using a single string resource: that's sufficient to demonstrate 
            for (var i = 0; i < 4; i++)
            {
                // pattern 1: use the ResourceContext from the UI thread
                string listItem1 = stringResourceMap.GetValue("string1", defaultContextForUiThread).ValueAsString;
                uiDependentResourceList.Add(listItem1);

                // pattern 2: use the view-independent ResourceContext
                string listItem2 = stringResourceMap.GetValue("string1", defaultViewIndependentResourceContext).ValueAsString;
                uiIndependentResourceList.Add(listItem2);
            }
        });

    // Display the results in one go. (A more finessed design might add results
    // in the UI asynchronously, but that goes beyond what this sample is 
    // demonstrating.)
    ViewDependentResourcesList.ItemsSource = uiDependentResourceList;
    ViewIndependentResourcesList.ItemsSource = uiIndependentResourceList;
}
void Scenario12::Scenario12Button_Show_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    // Two coding patterns will be used:
    //   1. Get a ResourceContext on the UI thread using GetForCurrentView and pass 
    //      to the non-UI thread
    //   2. Get a ResourceContext on the non-UI thread using GetForViewIndependentUse
    //
    // Two analogous patterns could be used for ResourceLoader instead of ResourceContext.

    // pattern 1: get a ResourceContext for the UI thread
    ResourceContext^ defaultContextForUiThread = ResourceContext::GetForCurrentView();

    // pattern 2: we'll create a view-independent context in the non-UI worker thread

    // We need some things in order to display results in the UI (doing that
    // for purposes of this sample, to show that work was actually done in the
    // worker thread): a pair of vectors to capture data, and a pair of variable 
    // references to the controls where the results will be displayed (needed to
    // pass to the .then lambda).
    Platform::Collections::Vector<Platform::String^>^ uiDependentResourceItems = ref new Platform::Collections::Vector<Platform::String^>();
    Platform::Collections::Vector<Platform::String^>^ uiIndependentResourceItems = ref new Platform::Collections::Vector<Platform::String^>();
    ItemsControl^ viewDependentListControl = ViewDependentResourcesList;
    ItemsControl^ viewIndependentListControl = ViewIndependentResourcesList;


    // use a worker thread for the heavy lifting so the UI isn't blocked
    concurrency::create_task(
        Windows::System::Threading::ThreadPool::RunAsync(
            ref new Windows::System::Threading::WorkItemHandler(
            [defaultContextForUiThread, uiDependentResourceItems, uiIndependentResourceItems](Windows::Foundation::IAsyncAction^ /*action*/)
        {
            // This is happening asynchronously on a background worker thread,
            // not on the UI thread.
            ResourceMap^ stringResourceMap = ResourceManager::Current->MainResourceMap->GetSubtree("Resources");

            // coding pattern 1: the defaultContextForUiThread variable was created above and has been captured to use here

            // coding pattern 2: get a view-independent ResourceContext
            ResourceContext^ defaultViewIndependentResourceContext = ResourceContext::GetForViewIndependentUse();

            // NOTE: The ResourceContext obtained using GetForViewIndependentUse() has no scale qualifier
            // value set. If this ResourceContext is used in its default state to retrieve a resource, that 
            // will work provided that the resource does not have any scale-qualified variants. But if the
            // resource has any scale-qualified variants, then that will fail at runtime.
            //
            // A scale qualifier value on this ResourceContext can be set programmatically. If that is done,
            // then the ResourceContext can be used to retrieve a resource that has scale-qualified variants.
            // But if the scale qualifier is reset (e.g., using the ResourceContext::Reset() method), then
            // it will return to the default state with no scale qualifier value set, and cannot be used
            // to retrieve any resource that has scale-qualified variants.

            // simulate processing a number of items
            // just using a single string resource: that's sufficient to demonstrate 
            for (auto i = 0; i < 4; i++)
            {
                // pattern 1: use the ResourceContext from the UI thread
                Platform::String^ item1 = stringResourceMap->GetValue("string1", defaultContextForUiThread)->ValueAsString;
                uiDependentResourceItems->Append(item1);

                // pattern 2: use the view-independent ResourceContext
                Platform::String^ item2 = stringResourceMap->GetValue("string1", defaultViewIndependentResourceContext)->ValueAsString;
                uiIndependentResourceItems->Append(item2);
            }
        }))
    ).then([uiDependentResourceItems, uiIndependentResourceItems, viewDependentListControl, viewIndependentListControl]
    {
        // After the async work is complete, this will execute on the UI thread.

        // Display the results in one go. (A more finessed design might add results
        // in the UI asynchronously, but that goes beyond what this sample is 
        // demonstrating.)
        viewDependentListControl->ItemsSource = uiDependentResourceItems;
        viewIndependentListControl->ItemsSource = uiIndependentResourceItems;
    });
}

설명

리소스는 규모에 민감할 수 있으며 앱이 소유한 다양한 보기는 서로 다른 배율을 사용할 수 있는 여러 디스플레이 디바이스에 동시에 표시할 수 있습니다. 이러한 이유로 ResourceContext는 일반적으로 특정 보기와 연결되며 GetForCurrentView를 사용하여 가져와야 합니다. (보기 독립적 ResourceContextGetForViewIndependentUse를 사용하여 가져올 수 있지만 뷰와 연결되지 않은 ResourceContext 에서 호출되면 크기 종속 기능이 실패합니다.)

더 이상 사용되지 않으며 이후 릴리스에서 제거될 수 있으므로 생성자를 사용하여 ResourceContext의 instance 만들지 마세요.

달리 언급된 경우를 제외하고 이 클래스의 메서드는 모든 스레드에서 호출할 수 있습니다.

버전 기록

Windows 버전 SDK 버전 추가된 값
1903 18362 GetForUIContext

생성자

ResourceContext()

복제된 ResourceContext 개체를 만듭니다.

참고

ResourceContext 생성자는 Windows 8.1 후 릴리스에 대해 변경되거나 사용할 수 없습니다. 대신 GetForCurrentView복제를 사용합니다.

속성

Languages

이 컨텍스트의 언어 한정자를 가져오거나 설정합니다.

QualifierValues

이름으로 인덱싱된 지원되는 모든 한정자의 쓰기 가능하고 관찰 가능한 맵을 가져옵니다.

메서드

Clone()

동일한 한정자를 사용하여 이 ResourceContext의 복제본을 만듭니다.

CreateMatchingContext(IIterable<ResourceQualifier>)

제공된 한정자 집합과 일치하는 새 ResourceContext 를 만듭니다.

참고

CreateMatchingContext는 Windows 8.1 후 릴리스에서 변경되거나 사용할 수 없습니다. 대신 ResourceContext.GetForCurrentView.OverrideToMatch를 사용합니다.

GetForCurrentView()

현재 실행 중인 애플리케이션의 현재 보기와 연결된 기본 ResourceContext 를 가져옵니다.

GetForUIContext(UIContext)

현재 실행 중인 애플리케이션에 대해 지정된 UIContext와 연결된 기본 ResourceContext를 가져옵니다.

GetForViewIndependentUse()

뷰와 연결되지 않은 기본 ResourceContext 를 가져옵니다.

OverrideToMatch(IIterable<ResourceQualifier>)

확인된 ResourceQualifier의 지정된 목록과 일치하도록 이 컨텍스트에서 제공하는 한정자 값을 재정의합니다. 일반적으로 확인된 ResourceQualifier는 이전에 조회한 리소스와 연결됩니다.

Reset()

지정된 ResourceContext instance 모든 한정자의 재정의된 값을 다시 설정합니다.

Reset(IIterable<String>)

지정된 ResourceContext instance 지정된 한정자의 재정의된 값을 다시 설정합니다.

ResetGlobalQualifierValues()

앱 전체의 모든 보기의 기본 컨텍스트에서 한정자 재정의를 제거합니다.

ResetGlobalQualifierValues(IIterable<String>)

앱 전체의 모든 보기의 기본 컨텍스트에서 지정된 한정자의 한정자 재정의를 제거합니다.

SetGlobalQualifierValue(String, String)

현재 앱에 대한 모든 보기의 기본 컨텍스트에 단일 한정자 값 재정의를 적용합니다.

SetGlobalQualifierValue(String, String, ResourceQualifierPersistence)

현재 앱에 대한 모든 뷰의 기본 컨텍스트에 단일 한정자 값 재정의를 적용하고 재정의의 지속성을 지정합니다.

적용 대상

추가 정보