ResourceContext Kelas

Definisi

Merangkum semua faktor (ResourceQualifiers) yang mungkin memengaruhi pemilihan sumber daya.

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
Warisan
Object Platform::Object IInspectable ResourceContext
Atribut

Persyaratan Windows

Rangkaian perangkat
Windows 10 (diperkenalkan dalam 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (diperkenalkan dalam v1.0)

Contoh

Contoh ini didasarkan pada skenario 12 sumber daya Aplikasi dan sampel pelokalan. Lihat sampel untuk solusi lengkap.

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;
    });
}

Keterangan

Sumber daya dapat sensitif terhadap skala, dan tampilan berbeda yang dimiliki oleh aplikasi dapat ditampilkan secara bersamaan pada perangkat tampilan yang berbeda, yang mungkin menggunakan skala yang berbeda. Untuk alasan itu, ResourceContext umumnya dikaitkan dengan tampilan tertentu, dan harus diperoleh menggunakan GetForCurrentView. ( ResourceContext yang independen tampilan dapat diperoleh menggunakan GetForViewIndependentUse, tetapi perhatikan bahwa fungsionalitas dependen skala akan gagal jika dipanggil pada ResourceContext yang tidak terkait dengan tampilan.)

Jangan membuat instans ResourceContext menggunakan konstruktor, karena tidak digunakan lagi dan tunduk pada penghapusan dalam rilis mendatang.

Kecuali jika dinyatakan lain, metode kelas ini dapat dipanggil pada utas apa pun.

Riwayat versi

Versi Windows Versi SDK Nilai ditambahkan
1903 18362 GetForUIContext

Konstruktor

ResourceContext()

Membuat objek ResourceContext kloning.

Catatan

Konstruktor ResourceContext dapat diubah atau tidak tersedia untuk rilis setelah Windows 8.1. Sebagai gantinya, gunakan GetForCurrentView dan Clone.

Properti

Languages

Mendapatkan atau mengatur kualifikasi bahasa untuk konteks ini.

QualifierValues

Mendapatkan peta yang dapat ditulis dan dapat diamati dari semua kualifikasi yang didukung, diindeks berdasarkan nama.

Metode

Clone()

Membuat klon resourcecontext ini, dengan kualifikasi yang identik.

CreateMatchingContext(IIterable<ResourceQualifier>)

Membuat ResourceContext baru yang cocok dengan sekumpulan kualifikasi yang disediakan.

Catatan

CreateMatchingContext mungkin diubah atau tidak tersedia untuk rilis setelah Windows 8.1. Sebagai gantinya, gunakan ResourceContext.GetForCurrentView.OverrideToMatch.

GetForCurrentView()

Mendapatkan ResourceContext default yang terkait dengan tampilan saat ini untuk aplikasi yang sedang berjalan.

GetForUIContext(UIContext)

Mendapatkan ResourceContext default yang terkait dengan UIContext yang ditentukan untuk aplikasi yang sedang berjalan.

GetForViewIndependentUse()

Mendapatkan ResourceContext default yang tidak terkait dengan tampilan apa pun.

OverrideToMatch(IIterable<ResourceQualifier>)

Mengambil alih nilai kualifikasi yang disediakan oleh konteks ini agar sesuai dengan daftar ResourceQualifieryang ditentukan. Biasanya ResourceQualifieryang diselesaikan dikaitkan dengan sumber daya yang telah dicari sebelumnya.

Reset()

Mengatur ulang nilai yang ditimpa untuk semua kualifikasi pada instans ResourceContext yang diberikan.

Reset(IIterable<String>)

Mengatur ulang nilai yang ditimpa untuk kualifikasi yang ditentukan pada instans ResourceContext yang diberikan.

ResetGlobalQualifierValues()

Menghapus semua penimpaan kualifikasi dari konteks default semua tampilan di seluruh aplikasi.

ResetGlobalQualifierValues(IIterable<String>)

Menghapus penimpaan kualifikasi untuk kualifikasi yang ditentukan dari konteks default semua tampilan di seluruh aplikasi.

SetGlobalQualifierValue(String, String)

Menerapkan penimpaan nilai kualifikasi tunggal ke konteks default semua tampilan untuk aplikasi saat ini.

SetGlobalQualifierValue(String, String, ResourceQualifierPersistence)

Menerapkan penimpaan nilai kualifikasi tunggal ke konteks default semua tampilan untuk aplikasi saat ini, dan menentukan persistensi penimpaan.

Berlaku untuk

Lihat juga