[源码下载]

背水一战 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. linux下进程间通信

    信号 信号是进程间相互传递消息的一种方法,只是用来通知某进程发生了什么事件,并不给进程传递任何数据. #include <sys/types.h> #include <unistd. ...

  2. Javascript模块化编程笔记

    最近在读阮一峰的博客http://www.ruanyifeng.com/blog/2012/10/javascript_module.html,随手记录一些重要笔记.  Javascript模块的雏形 ...

  3. Canvas 内部元素添加事件处理

    目录 前言 自定义事件 有序数组 元素父类 事件判断 其他 立即执行函数 apply, call, bind addEventListener 传参 调用父类的构造函数 对象检测 isPointInP ...

  4. atitit。ocr框架类库大全 attilax总结

    atitit.ocr框架类库大全 attilax总结 Tesseract Asprise JavaOCR 闲来无事,发现百度有一个OCR文字识别接口,感觉挺有意思的,拿来研究一下. 百度服务简介:文字 ...

  5. PHP实现RESTful风格的API实例(三)

    接前一篇PHP实现RESTful风格的API实例(二) .htaccess :重写URL,使URL以 /restful/class/1 形式访问文件 Options +FollowSymlinks R ...

  6. 【java并发】传统线程技术中创建线程的两种方式

    传统的线程技术中有两种创建线程的方式:一是继承Thread类,并重写run()方法:二是实现Runnable接口,覆盖接口中的run()方法,并把Runnable接口的实现扔给Thread.这两种方式 ...

  7. c# BlowFish 高速 对称加密

    BlowFish 高速 对称加密 string key = "this is my key"; BlowFish algo = new BlowFish(key); string ...

  8. 【.net】从比较两个字节数组谈起

    上午,有位初学者朋友问:如何比较两个字节数组中各字节是否相等? 不许笑,我一向反对嘲笑初学者,初学者不认真学习时你可以批评,但不能讥嘲.你不妨想想,你自己开始学习编程的时候又是什么个光景? 好,于是, ...

  9. struts2拦截器

    一.自定义拦截器 struts2拦截器类似于servlet过滤器 首先定义一个拦截器这个拦截器实现了Interceptor接口: package cn.orlion.interceptor; impo ...

  10. poj 2226 Muddy Fields(合理建图+二分匹配)

    /* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...