ResourceContext 类

ResourceContext Class

封装可能影响资源选定的所有因素 (ResourceQualifiers)。

语法


var resourceContext = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView();

特性

[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]

成员

ResourceContext类 具有这些类型的成员:

构造函数

ResourceContext类 具有这些构造函数。

构造函数说明
ResourceContext Creates a cloned ResourceContext object.

 

方法

The ResourceContext 类 具有这些方法。 对于 C#、Visual Basic 和 C++,它还继承方法于Object 类.

方法说明
Clone Creates a clone of this ResourceContext, with identical qualifiers.
CreateMatchingContext Creates a new ResourceContext that matches a supplied set of qualifiers.
GetForCurrentView Gets a default ResourceContext associated with the current view for the currently running application.
GetForViewIndependentUse Gets a default ResourceContext not associated with any view.
OverrideToMatch 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.
Reset() Resets the overridden values for all qualifiers on the given ResourceContext instance.
Reset(IIterable(String)) Resets the overridden values for the specified qualifiers on the given ResourceContext instance.
ResetGlobalQualifierValues() Removes any qualifier overrides from default contexts of all views across the app.
ResetGlobalQualifierValues(IIterable(String)) Removes qualifier overrides for the specified qualifiers from default contexts of all views across the app.
SetGlobalQualifierValue Applies a single qualifier value override to default contexts of all views for the current app.

 

属性

ResourceContext类 具有这些属性。

属性访问类型说明

Languages

读/写Gets or sets the language qualifier for this context.

QualifierValues

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

 

示例

此示例基于 应用程序资源和本地化示例 的方案 13。查看更多完整解决方案的示例。


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

要求

最低支持客户端

Windows 8 [仅 Windows 应用商店应用程序]

最低支持服务器

Windows Server 2012 [仅 Windows 应用商店应用程序]

命名空间

Windows.ApplicationModel.Resources.Core
Windows::ApplicationModel::Resources::Core [C++]

元数据

Windows.winmd

另请参见

ResourceQualifier
应用程序资源和本地化示例

 

 

显示:
© 2018 Microsoft