ResourceCandidate 类

定义

表示给定 NamedResource 的单个可能值、与该资源关联的限定符,以及这些限定符与解析它的上下文的匹配程度。

public ref class ResourceCandidate sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class ResourceCandidate final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class ResourceCandidate
Public NotInheritable Class ResourceCandidate
继承
Object Platform::Object IInspectable ResourceCandidate
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

此示例基于 应用程序资源和本地化示例的方案 10。 有关完整解决方案,请参阅示例。

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";
            j++;
        }
        i++;
    }

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

注解

版本历史记录

Windows 版本 SDK 版本 已添加值
1903 18362 种类

属性

IsDefault

指示此 ResourceCandidate 是否可用作任何上下文的默认回退值。

IsMatch

指示此 ResourceCandidate 是否与计算它所依据的 ResourceContext 匹配。

IsMatchAsDefault

指示此 ResourceCandidate 是否与作为真实匹配项、默认回退值或具有默认回退 (的混合匹配(即,相对于某些限定符的匹配,但对其他限定符使用默认回退值) )所针对的 ResourceContext 匹配。

Kind

获取此 ResourceCandidate 中封装的资源的类型。

Qualifiers

获取与此 ResourceCandidate 关联的限定符。

ValueAsString

获取此 ResourceCandidate 的值,以字符串表示。

方法

GetQualifierValue(String)

返回限定符的值,给定其名称。

GetValueAsFileAsync()

异步返回一个 StorageFile ,用于访问此 ResourceCandidate 的值。 这仅适用于属于文件的某些类型的 ResourceCandidate

GetValueAsStreamAsync()

异步返回访问此 ResourceCandidate 的值的 IRandomAccessStream

适用于

另请参阅