ResourceContext ResourceContext ResourceContext ResourceContext ResourceContext Class

Definition

Encapsulates all of the factors (ResourceQualifier s) that might affect resource selection.

public : sealed class ResourceContext : IResourceContext
struct winrt::Windows::ApplicationModel::Resources::Core::ResourceContext : IResourceContext
public sealed class ResourceContext : IResourceContext
Public NotInheritable Class ResourceContext Implements IResourceContext
var resourceContext = new resourceContext();
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

This example is based on scenario 13 of the Application resources and localization sample. See the sample for the more complete solution.

// Create a ResourceContext.
var resourceContext = new Windows.ApplicationModel.Resources.Core.ResourceContext.getForCurrentView().clone();

// Set the specific context for lookup of resources.
var qualifierValues = resourceContext.qualifierValues;
qualifierValues["language"] = "en-US";
qualifierValues["contrast"] = "standard";
qualifierValues["scale"] = "140";
qualifierValues["homeregion"] = "021"; // Northern America

// Resources actually reside within Scenario13 Resource Map.
var resourceIds = [
    '/Scenario13/languageOnly',
    '/Scenario13/scaleOnly',
    '/Scenario13/contrastOnly',
    '/Scenario13/homeregionOnly',
    '/Scenario13/multiDimensional',
];

var output = { str: "" };
resourceIds.forEach(function (resourceId) {
    renderNamedResource(resourceId, resourceContext, output);
});

function renderNamedResource(resourceId, resourceContext, output) {
    output.str += "Resource ID " + resourceId + ":\n";
    // Lookup the resource in the mainResourceMap (the one for this package).
    var namedResource = Windows.ApplicationModel.Resources.Core.ResourceManager.current.mainResourceMap.lookup(resourceId);

    // Return a ResourceCandidateVectorView of all possible resources candidates
    // resolved against the context in order of appropriateness.
    var resourceCandidates = namedResource.resolveAll(resourceContext);

    resourceCandidates.forEach(function (candidate, index) {
        renderCandidate(candidate, index, output);
    });
    output.str += "\n";
}

function renderCandidate(candidate, index, output) {
    // Get all the various qualifiers for the candidate (such as language, scale, contrast).
    candidate.qualifiers.forEach(function (qualifier) {
        output.str += "qualifierName: " + qualifier.qualifierName + "\n";
        output.str += "qualifierValue: " + qualifier.qualifierValue + "\n";
        output.str += "isDefault: ";
        output.str += (qualifier.isDefault) ? "true\n" : "false\n";
        output.str += "isMatch: ";
        output.str += (qualifier.isMatch) ? "true\n" : "false\n";
        output.str += "score: " + qualifier.score + "\n";
        output.str += "\n";
    });
}

Remarks

Resources can be sensitive to scale, and different views owned by an app are able to display simultaneously on different display devices, which might use different scales. For that reason, a ResourceContext is generally associated with a specific view, and should be obtained using GetForCurrentView. (A view-independent ResourceContext can be obtained using GetForViewIndependentUse, but note that scale-dependent functionality will fail if invoked on a ResourceContext that is not associated with a view.)

Do not create an instance of ResourceContext using the constructor, as it is deprecated and subject to removal in a future release.

Except where otherwise noted, methods of this class can be called on any thread.

Constructors

ResourceContext() ResourceContext() ResourceContext() ResourceContext() ResourceContext()

Note

ResourceContext constructor may be altered or unavailable for releases after Windows 8.1. Instead, use GetForCurrentView.

Creates a cloned ResourceContext object.

Properties

Languages Languages Languages Languages Languages

Gets or sets the language qualifier for this context.

QualifierValues QualifierValues QualifierValues QualifierValues QualifierValues

Gets a writable, observable map of all supported qualifiers, indexed by name.

Methods

Clone() Clone() Clone() Clone() Clone()

Creates a clone of this ResourceContext, with identical qualifiers.

CreateMatchingContext(IIterable<ResourceQualifier>) CreateMatchingContext(IIterable<ResourceQualifier>) CreateMatchingContext(IIterable<ResourceQualifier>) CreateMatchingContext(IIterable<ResourceQualifier>) CreateMatchingContext(IIterable<ResourceQualifier>)

Note

CreateMatchingContext may be altered or unavailable for releases after Windows 8.1. Instead, use ResourceContext.GetForCurrentView.OverrideToMatch.

Creates a new ResourceContext that matches a supplied set of qualifiers.

GetForCurrentView() GetForCurrentView() GetForCurrentView() GetForCurrentView() GetForCurrentView()

Gets a default ResourceContext associated with the current view for the currently running application.

GetForViewIndependentUse() GetForViewIndependentUse() GetForViewIndependentUse() GetForViewIndependentUse() GetForViewIndependentUse()

Gets a default ResourceContext not associated with any view.

OverrideToMatch(IIterable<ResourceQualifier>) OverrideToMatch(IIterable<ResourceQualifier>) OverrideToMatch(IIterable<ResourceQualifier>) OverrideToMatch(IIterable<ResourceQualifier>) OverrideToMatch(IIterable<ResourceQualifier>)

Overrides the qualifier values supplied by this context to match a specified list of resolved ResourceQualifier s. Typically the resolved ResourceQualifier s are associated with a resource that was looked up earlier.

Reset() Reset() Reset() Reset() Reset()

Resets the overridden values for all qualifiers on the given ResourceContext instance.

Reset(IIterable<String>) Reset(IIterable<String>) Reset(IIterable<String>) Reset(IIterable<String>) Reset(IIterable<String>)

Resets the overridden values for the specified qualifiers on the given ResourceContext instance.

ResetGlobalQualifierValues() ResetGlobalQualifierValues() ResetGlobalQualifierValues() ResetGlobalQualifierValues() ResetGlobalQualifierValues()

Removes any qualifier overrides from default contexts of all views across the app.

ResetGlobalQualifierValues(IIterable<String>) ResetGlobalQualifierValues(IIterable<String>) ResetGlobalQualifierValues(IIterable<String>) ResetGlobalQualifierValues(IIterable<String>) ResetGlobalQualifierValues(IIterable<String>)

Removes qualifier overrides for the specified qualifiers from default contexts of all views across the app.

SetGlobalQualifierValue(String, String) SetGlobalQualifierValue(String, String) SetGlobalQualifierValue(String, String) SetGlobalQualifierValue(String, String) SetGlobalQualifierValue(String, String)

Applies a single qualifier value override to default contexts of all views for the current app.

SetGlobalQualifierValue(String, String, ResourceQualifierPersistence) SetGlobalQualifierValue(String, String, ResourceQualifierPersistence) SetGlobalQualifierValue(String, String, ResourceQualifierPersistence) SetGlobalQualifierValue(String, String, ResourceQualifierPersistence) SetGlobalQualifierValue(String, String, ResourceQualifierPersistence)

Applies a single qualifier value override to default contexts of all views for the current app, and specifies the persistence of the override.

See Also