• 资源: 资源限定符概述, 资源限定符示例


    资源限定符概述

    • 资源限定符示例



    示例
    1、资源限定符概述
    Resource/Qualifiers/Summary.xaml

    复制代码
    <Page
        x:Class="Windows10.Resource.Qualifiers.Summary"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Windows10.Resource.Qualifiers"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="Transparent">
            <StackPanel Margin="10 0 10 10">
    
                <TextBlock Name="lblMsg" Margin="0 10 0 0" />
    
            </StackPanel>
        </Grid>
    </Page>
    复制代码

    Resource/Qualifiers/Summary.xaml.cs

    复制代码
    /*
     * qualifiers - 限定符,在 uwp 中支持通过限定符来命名资源。限定符用于标识某个资源文件使用场景的上下文
     *
     * 本例用于演示如何获取系统支持的全部限定符
     * 关于限定符的规则及示例参见 /Resource/Qualifiers/Demo.xaml
     */
    
    using System;
    using Windows.ApplicationModel.Resources.Core;
    using Windows.Foundation.Collections;
    using Windows.Graphics.Display;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace Windows10.Resource.Qualifiers
    {
        public sealed partial class Summary : Page
        {
            public Summary()
            {
                this.InitializeComponent();
    
                this.Loaded += Summary_Loaded;
            }
    
            private void Summary_Loaded(object sender, RoutedEventArgs e)
            {
                // 列举出系统支持的全部限定符,及其对应的值
                IObservableMap<string, string> qualifiers = ResourceContext.GetForCurrentView().QualifierValues;
                foreach (var qualifier in qualifiers)
                {
                    lblMsg.Text += string.Format("{0}: {1}", qualifier.Key, qualifier.Value);
                    lblMsg.Text += Environment.NewLine;
                }
                lblMsg.Text += Environment.NewLine;
    
                // 常用的有:Scale, DeviceFamily, Language, TargetSize, 其他的都不常用
    
                // 获取当前的缩放比例
                string scale;
                ResourceContext.GetForCurrentView().QualifierValues.TryGetValue("Scale", out scale);
                lblMsg.Text += "缩放比例: " + scale;
                lblMsg.Text += Environment.NewLine;
    
                // 获取当前的缩放比例(Windows.Graphics.Display.ResolutionScale 枚举)
                lblMsg.Text += "ResolutionScale: " + DisplayInformation.GetForCurrentView().ResolutionScale;
                lblMsg.Text += Environment.NewLine;
    
                // 获取当前的设备类型
                string deviceFamily;
                ResourceContext.GetForCurrentView().QualifierValues.TryGetValue("DeviceFamily", out deviceFamily);
                lblMsg.Text += "设备类型: " + deviceFamily;
                lblMsg.Text += Environment.NewLine;
    
                // 获取当前的语言类型
                string language;
                ResourceContext.GetForCurrentView().QualifierValues.TryGetValue("Language", out language);
                lblMsg.Text += "语言类型: " + language;
                lblMsg.Text += Environment.NewLine;
            }
        }
    }
    复制代码


    2、资源限定符示例
    Resource/Qualifiers/Demo.xaml

    复制代码
    <Page
        x:Class="Windows10.Resource.Qualifiers.Demo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Windows10.Resource.Qualifiers"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="Transparent">
            <StackPanel Margin="10 0 10 10">
    
                <TextBlock Name="lblMsg" Margin="5" />
                
                <!--
                    限定符用于标识某个资源文件使用场景的上下文,本例通过 DeviceFamily 和 Scale 两种限定符来演示实际效果(关于系统支持的全部限定符参见 /Qualifiers/Summary.xaml),规则总结如下
                    1、命名规则
                        a) 文件夹式: folder/qualifiername-value/qualifiername-value/filename.ext 或者 folder/qualifiername-value/qualifiername-value_qualifiername-value/filename.ext 等
                        b) 文件名式: folder/filename.qualifiername-value_qualifiername-value.ext 等(多个限定符之间用“_”分隔)
                        c) 文件夹式和文件名式可以混合使用
                    2、调用规则: 直接引用 folder/filename.ext,系统将自动根据限定符指定的上下文做匹配
                    3、无论是资源名,还是限定符都不区分大小写
                    4、同一个资源的限定符不能重复,否则编译报错。比如 folder/filename.scale-100.png 和 folder/scale-100/filename.png 其实是同名限定符资源,不能共存
                    5、对于非 scale 限定符资源,如果匹配不到,系统则会去调用对应的无限定符资源
                    6、对于 scale 限定符资源,只要有一个 scale 资源就不会去调用对应的无限定符资源。当无法精确匹配时,系统先去找临近的更大比例的,找不到的话再按从大到小的顺序找小比例的
                    7、语言限定符有一个特殊性,其限定符名称可以是 language 或 lang,用文件夹式的话是不需要限定符名称的。比如文件名式 filename.language-en-US.png 或 filename.lang-en-US.png 对应的文件夹式为 en-US/filename.png
                
                    另外:限定符 TargetSize 用于限定图标的大小,不能和 Scale 共存
                    1、TargetSize 主要用于桌面 Windows 资源管理器中显示的文件类型关联图标或协议图标
                    2、TargetSize 限定的是一个正方形图标,其值的单位是像素,类似 filename.targetsize-16.png, filename.targetsize-32.png 等
                    3、当无法精确匹配时,系统先去找临近的更大像素的,找不到的话再按从大到小的顺序找小像素的
                    4、去 Package.appxmanifest 的“可见资产”看看,标识为“比例”的对应的是 Scale 限定符,标识为“目标大小”的对应的是 TargetSize 限定符
                -->
    
                <StackPanel Orientation="Horizontal">
                    <Image Source="/Resource/Qualifiers/myImage1.png" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" />
                    <Image Source="/Resource/Qualifiers/myImage2.png" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" />
                </StackPanel>
    
                <StackPanel Orientation="Horizontal" Margin="0 10 0 0">
                    <Image Source="/Resource/Qualifiers/myImage3.png" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" />
                    <Image Source="/Resource/Qualifiers/myImage4.png" Width="100" Height="100" HorizontalAlignment="Left" Margin="5" />
                </StackPanel>
    
            </StackPanel>
        </Grid>
    </Page>
    复制代码

    Resource/Qualifiers/Demo.xaml.cs

    复制代码
    /*
     * 本例用于演示限定符的实际效果
     */
    
    using System;
    using Windows.Graphics.Display;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace Windows10.Resource.Qualifiers
    {
        public sealed partial class Demo : Page
        {
            public Demo()
            {
                this.InitializeComponent();
    
                this.Loaded += Demo_Loaded;
            }
    
            private void Demo_Loaded(object sender, RoutedEventArgs e)
            {
                // 获取当前的缩放比例
                string scale;
                Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Scale", out scale);
                lblMsg.Text += "缩放比例: " + scale;
                lblMsg.Text += Environment.NewLine;
    
                // 获取当前的缩放比例(Windows.Graphics.Display.ResolutionScale 枚举)
                lblMsg.Text += "ResolutionScale: " + DisplayProperties.ResolutionScale.ToString();
                lblMsg.Text += Environment.NewLine;
    
                // 获取当前的设备类型
                string deviceFamily;
                Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("DeviceFamily", out deviceFamily);
                lblMsg.Text += "设备类型: " + deviceFamily;
                lblMsg.Text += Environment.NewLine;
    
                
            }
        }
    }
    复制代码


    注:本例的资源文件结构

    复制代码
    |Resource
    |--Qualifiers
    |----deviceFamily-desktop
    |------scale-100
    |--------MyImage4.png
    |----deviceFamily-mobile
    |------MyImage4.scale-100.png
    |----scale-100
    |------MyImage1.png
    |----scale-140
    |------MyImage1.png
    |----scale-180
    |------MyImage1.png
    |----MyImage1.png
    |----MyImage2.png
    |----MyImage2.scale-100.png
    |----MyImage2.scale-140.png
    |----MyImage2.scale-180.png
    |----MyImage3.DeviceFamily-Desktop_scale-100.png
    |----MyImage3.DeviceFamily-Mobile_scale-100.png
    |----MyImage3.png
    |----MyImage4.png
    复制代码
  • 相关阅读:
    C语言I博客作业04
    解决@ResponseBody注解返回的json中文乱码问题
    自定义 Helper Method
    webapi 学习1
    分布式事务TransactionOptions及设置msdtc
    了解websocket是什么
    用postman 来实现post方式返回json数据
    异步 Controller
    asp.net mvc Filter
    asp.net mvc Controller Factory
  • 原文地址:https://www.cnblogs.com/ansen312/p/5913511.html
Copyright © 2020-2023  润新知