[源码下载]

背水一战 Windows 10 (9) - 资源: 资源限定符概述, 资源限定符示例

作者:webabcd

介绍
背水一战 Windows 10 之 资源

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

示例
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.ApplicationModel.Resources.Core;
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;
ResourceContext.GetForCurrentView().QualifierValues.TryGetValue("Scale", out scale);
lblMsg.Text += "缩放比例: " + scale;
lblMsg.Text += Environment.NewLine; // 获取当前的缩放比例(Windows.Graphics.Display.ResolutionScale 枚举)
lblMsg.Text += "ResolutionScale: " + DisplayInformation.GetForCurrentView().ResolutionScale.ToString();
lblMsg.Text += Environment.NewLine; // 获取当前的设备类型
string deviceFamily;
ResourceContext.GetForCurrentView().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

OK
[源码下载]

背水一战 Windows 10 (9) - 资源: 资源限定符概述, 资源限定符示例的更多相关文章

  1. 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement

    [源码下载] 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) Im ...

  2. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  3. 背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox

    [源码下载] 背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) T ...

  4. 背水一战 Windows 10 (115) - 后台任务: 通过 toast 激活后台任务, 定时激活后台任务

    [源码下载] 背水一战 Windows 10 (115) - 后台任务: 通过 toast 激活后台任务, 定时激活后台任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 通 ...

  5. 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景

    [源码下载] 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景 作者:webabcd 介绍背水一战 Windows 10 之 通知(Toast) 提示音 特定场 ...

  6. 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater

    [源码下载] 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater 作者:webabcd 介绍背水一战 Windows 10 之 选取器 CachedFileUp ...

  7. 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar

    [源码下载] 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) App ...

  8. 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画

    [源码下载] 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 动画 示例An ...

  9. 背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView

    [源码下载] 背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView 作者:webabcd 介绍背水一战 Windows 1 ...

随机推荐

  1. axis

    http://www.cnblogs.com/liyanblog/archive/2011/11/29/2266942.html 报错: D:\ws\la\WSofSMNS\WebRoot\WEB-I ...

  2. SQL中group by的用法

    group by即按照给定字段对结果集进行分组,从字面意义上理解就是根据"by"指定的规则对数据进行分组,所谓的分组就是将一个"数据集"划分成若干个" ...

  3. fir.im Weekly - 你与优秀源码之间只差一个 Star

    说起开源社区,Github 是一个不可缺少的存在.作为全球最大的同性交友网站,上面有太多优秀的开源代码库和编程大神,让无数开发者心生向往.那么如何正确的使用 Github,也许是编程学习之必要.来看下 ...

  4. 据说,每一个 iOSer 都想要一张 Swift 大会门票

    据说,每一个 iOSer 都想要一张中国首届 Swift 开发者大会的门票: 那么,福利来了-- fir.im 作为中国首届 Swift 大会的唯一钻石赞助商,有最后 2 张价值 600 多的门票(已 ...

  5. SQL*Loader之CASE5

    CASE5 1. SQL脚本 [oracle@node3 ulcase]$ cat ulcase5.sql set termout off rem host write sys$output &quo ...

  6. tomcat触发ServletContext初始化监听事件的源码(原创)

    tomcat 8.0.36 知识点: 动态监听器的好处可以根据环境条件进行选择性添加. 静态监听器有七类. ServletContextAttributeListener ServletRequest ...

  7. MySQL的学习--用户创建授权

    前一段时间,将项目改成SAAS的架构,每个billing account都可以获得一个子域,一个单独的数据库,一个单独的数据库用户和对应数据库的权限. 现在有时间了,将数据库相关的命令用博客备份一下. ...

  8. static 关键字

    static对象如果出现在类中,那么该对象即使从未被使用到,它也会被构造以及析构.而函数中的static对象,如果该函数从未被调用,这个对象也就绝不会诞生,但是在函数每次被调用时检查对象是否需要诞生. ...

  9. Tomcat7.0启动报错:java.lang.illegalargumentexception:taglib definition not consisten with specification version

    Tomcat7.0启动报错:java.lang.illegalargumentexception:taglib definition not consisten with specification ...

  10. iOS_UIImage_Gif的分解

    /** Gif的步骤 1. 拿到Gifd的数据 2. 将Gif分解为一帧帧 3. 将单帧数据转为UIImage 4. 单帧图片保存 */ github地址: https://github.com/ma ...