ResourceContext Class


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

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)]
public sealed class ResourceContext
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class ResourceContext
function ResourceContext()
Public NotInheritable Class ResourceContext
Object IInspectable ResourceContext

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)


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

private void Scenario10Button_Show_Click(object sender, RoutedEventArgs e)
    Button b = sender as Button;
    if (b != null)
        // Use a cloned context for this scenario so that qualifier values set
        // in this scenario don't impact behavior in other scenarios that
        // use a default context for the view (crossover effects between
        // the scenarios will not be expected).
        var context = ResourceContext.GetForCurrentView().Clone();

        var selectedLanguage = Scenario10ComboBox_Language.SelectedValue;
        var selectedScale = Scenario10ComboBox_Scale.SelectedValue;
        var selectedContrast = Scenario10ComboBox_Contrast.SelectedValue;
        var selectedHomeRegion = Scenario10ComboBox_HomeRegion.SelectedValue;

        if (selectedLanguage != null)
            context.QualifierValues["language"] = selectedLanguage.ToString();
        if (selectedScale != null)
            context.QualifierValues["scale"] = selectedScale.ToString();
        if (selectedContrast != null)
            context.QualifierValues["contrast"] = selectedContrast.ToString();
        if (selectedHomeRegion != null)
            context.QualifierValues["homeregion"] = selectedHomeRegion.ToString();
        Scenario10_SearchMultipleResourceIds(context, new string[] { "LanguageOnly", "ScaleOnly", "ContrastOnly", "HomeRegionOnly", "MultiDimensional" });

void Scenario10_SearchMultipleResourceIds(ResourceContext context, string[] resourceIds)
    this.Scenario10TextBlock.Text = "";
    var dimensionMap = ResourceManager.Current.MainResourceMap.GetSubtree("dimensions");

    foreach (var id in resourceIds)
        NamedResource namedResource;
        if (dimensionMap.TryGetValue(id, out namedResource))
            var resourceCandidates = namedResource.ResolveAll(context);
            Scenario10_ShowCandidates(id, resourceCandidates);

void Scenario10_ShowCandidates(string resourceId, IReadOnlyList<ResourceCandidate> resourceCandidates)
    // Print 'resourceId', 'found value', 'qualifer info', 'matching condition'
    string outText = "resourceId: dimensions\\" + resourceId + "\r\n";
    int i = 0;
    foreach (var candidate in resourceCandidates)
        var value = candidate.ValueAsString;
        outText += "    Candidate " + i.ToString() + ":" + value + "\r\n";
        int j = 0;
        foreach (var qualifier in candidate.Qualifiers)
            var qualifierName = qualifier.QualifierName;
            var qualifierValue = qualifier.QualifierValue;
            outText += "        Qualifer: " + qualifierName + ": " + qualifierValue + "\r\n";
            outText += "        Matching: IsMatch (" + qualifier.IsMatch.ToString() + ")  " + "IsDefault (" + qualifier.IsDefault.ToString() + ")" + "\r\n";

    this.Scenario10TextBlock.Text += outText + "\r\n";


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.

Version history

Windows version SDK version Value added
1903 18362 GetForUIContext



Creates a cloned ResourceContext object.


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



Gets or sets the language qualifier for this context.


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



Creates a clone of this ResourceContext, with identical qualifiers.


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


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


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


Gets a default ResourceContext associated with the specified UIContext for the currently running application.


Gets a default ResourceContext not associated with any view.


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


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


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


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


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

SetGlobalQualifierValue(String, String)

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

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.

Applies to

See also