背水一战 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) - 资源: 资源限定符概述, 资源限定符示例的更多相关文章
- 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement
[源码下载] 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) Im ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
- 背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox
[源码下载] 背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) T ...
- 背水一战 Windows 10 (115) - 后台任务: 通过 toast 激活后台任务, 定时激活后台任务
[源码下载] 背水一战 Windows 10 (115) - 后台任务: 通过 toast 激活后台任务, 定时激活后台任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 通 ...
- 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景
[源码下载] 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景 作者:webabcd 介绍背水一战 Windows 10 之 通知(Toast) 提示音 特定场 ...
- 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater
[源码下载] 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater 作者:webabcd 介绍背水一战 Windows 10 之 选取器 CachedFileUp ...
- 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar
[源码下载] 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) App ...
- 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画
[源码下载] 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 动画 示例An ...
- 背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView
[源码下载] 背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView 作者:webabcd 介绍背水一战 Windows 1 ...
随机推荐
- paip.2013年技术趋势以及热点 v2.0 cae
paip.2013年技术趋势以及热点 v2.0 cae HTML5 多核编程 物联网 可穿戴计算设备 3. 物联网 无论是M2M(机器对机器)通信应用,还是NFC(进距离通信)技术,都是物联网的组成部 ...
- 简单的JPA注解例子
package ssh.entity; import java.math.BigDecimal; import java.util.Date; import javax.persistence.*; ...
- 8 步搭建 Node.js + MongoDB 项目的自动化持续集成
任何事情超过 90 秒就应该自动化,这是程序员的终极打开方式.Automating shapes smarter future. 这篇文章中,我们通过创建一个 Node.js + MongoDB 项目 ...
- linux 学习 设置固定网Ip
本人使用CentOs6.5 最近在学习linux操作系统,单在使用shell连接前都要使用ifconfig eth0 设置一个临时IP让我不胜其烦.决定学习设置一个固定IP 步骤: 1.登录计算机后使 ...
- jquerymobile仿微信 - 01
jquerymobile仿微信 - 01 jquerymobile的组件感觉不咋地哇 本地调试最好是开一个web server,不然数据访问会有问题 <div data-role="p ...
- KnockoutJS 3.X API 第五章 高级应用(3) 虚拟元素绑定
注意:这是一种高级技术,通常仅在创建可重用绑定的库时使用. 这不是你通常需要做的时候使用Knockout构建应用程序. Knockout的控制流绑定(例如,if和foreach)不仅可以应用于常规DO ...
- Python之文件读写
本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 Python文件操作步骤示例 Python文件读取相关方法 文件读写与字符编码 一.I/O操作概述 I/O在计算机中是指Input ...
- ASP.NET MVC在线人数统计
在Global.asax.cs文件中代码: protected void Application_Start() { Application[; AreaRegistration.RegisterAl ...
- 【WP 8.1开发】解决调用真实摄像头会死机的问题
无论你是用Silverlight还是用RT的API来开发,在使用MediaCapture拍照片或录视频时,要是在模拟器上运行会万事大吉:但是,一旦放到真实手机上运行,肯定有人发现了,细心的朋友肯定发现 ...
- hibernate(八)一对多关联
一.一对多单向关良 一对多单向关联与多对一相似 假设一个组有多个用户,即一(Group)对多(User) 需要在Group类中添加一个User类的Set集合(数据库中的用户不可能是重复的,所以要用Se ...