2019-5-21-win10-uwp-xaml-兼容多个版本条件编译
| title | author | date | CreateTime | categories |
|---|---|---|---|---|
|
win10 uwp xaml 兼容多个版本条件编译
|
lindexi
|
2019-5-21 11:19:3 +0800
|
2018-03-18 15:37:54 +0800
|
Win10 UWP
|
在 UWP 开发有一个坑就是存在很多SDK的版本,同时不同的系统带的SDK是不相同的,还好现在高版本的系统是可以支持低版本的程序的。为了做到尽可能兼容,程序需要用到足够低的 SDK 版本,但是又存在很多新版本特性非常好用,那么如何在用户端判断当前的系统是哪个版本对应可以使用新版本的特性?当然这个代码在后台代码一点都不难,但是界面呢?本文告诉大家如何设置 xaml 的条件编译
如果只需要在 cs 代码判断版本,那么可以使用星期大神的代码,请看UWP 判断系统版本
public class VersionsHelper
{
public static Boolean Windows10Build10240 => ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 1, 0);
public static Boolean Windows10Build10586 => ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 2, 0);
public static Boolean Windows10Build14393 => ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 3, 0);
public static Boolean Windows10Build15063 => ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 4, 0);
public static Boolean Windows10Build16299 => ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5, 0);
}
但是如果是在 xaml 应该怎么写? 我需要使用 16299 的功能,但是我需要让程序可以在 15063 运行,那么这时就需要 uwp xmal 条件编译。
使用的方法很简单,不过条件编译不是和 cs 代码使用 #if 的方式。
这里的 xaml 条件编译(Conditional XAML)就是 ApiInformation.IsApiContractPresent 提供的标记。
因为这个标记稍微有些复杂,所以我先写代码给大家看
xmlns:contract5NotPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract,5)"
xmlns:contract5Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"
<TextBlock Margin="200,200,0,0" contract5NotPresent:Text="以前的系统"
contract5Present:Text="最新的系统"></TextBlock>
因为xaml条件编译是在创意者更新 15063 支持的,所以需要先右击属性,设置最低版本为 15063,然后才可以编译
因为我的系统是 16299 所以运行就是显示最新的系统,如果是在 15063 的系统运行,因为我自己没运行,所以运行显示的我也不知道。
下面让我来告诉大家是如何写的。
首先在命名定义一个变量,这个变量是可以随意写的,我是直接抄微软代码,所以写为contract5Present ,他的值就是 http://schemas.microsoft.com/winfx/2006/xaml/presentation ,因为微软支持在最后面添加函数,所以加上函数IsApiContractPresent 就是这样写
xmlns:contract5Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"
那么需要来告诉大家 IsApiContractPresent 是做什么?
如果大家有打开 UWP 判断系统版本那么会发现判断系统的方法是通过最后的数字。
对应的数字
1:10240
2:10586
3:14393
4:15063
5:16299
如果在运行比数字低的版本,会返回true,例如 在运行 15063 的系统,可以看到下面的代码返回的值
IsApiContractPresent(Windows.Foundation.UniversalApiContract, 5) = false // 不属于 16299
IsApiContractPresent(Windows.Foundation.UniversalApiContract, 4) = true // 属于 15063
IsApiContractPresent(Windows.Foundation.UniversalApiContract, 3) = true // 属于 14393
IsApiContractPresent(Windows.Foundation.UniversalApiContract, 2) = true // 属于 10586
IsApiContractPresent(Windows.Foundation.UniversalApiContract, 1) = true // 属于 10240
是的,如果运行在 16299 版本的系统上,那么后面写 12345 都是返回true 所以在最高返回 true 的版本就是当前系统可以支持的版本。在调用 IsApiContractPresent 方法,如果返回 true 那么设置的属性才可以。如果返回 false 那么在运行就不会有设置。
但是不能这样写代码
<TextBlock Text="Hello, World" contract5Present:Text="Hello, Conditional XAML"/>
如果需要判断在 16299 就设置这个属性,而在非 16299 就不设置这个属性,就需要使用IsApiContractNotPresent 对比一下,如果在 15063 的系统运行程序,那么下面代码就是这个值
- IsApiContractNotPresent(Windows.Foundation.UniversalApiContract, 5) = true
- IsApiContractNotPresent(Windows.Foundation.UniversalApiContract, 4) = false
- IsApiContractNotPresent(Windows.Foundation.UniversalApiContract, 3) = false
- IsApiContractNotPresent(Windows.Foundation.UniversalApiContract, 2) = false
- IsApiContractNotPresent(Windows.Foundation.UniversalApiContract, 1) = false
IsApiContractPresent 是在当前系统和低于当前系统返回 true ,IsApiContractNotPresent 是在非当前系统和不低于当前系统返回 true
所以使用IsApiContractPresent和IsApiContractNotPresent可以设置当前使用的是那个版本
例如使用下面的代码,用到了 RevealBorderBrush 这个新功能
<RevealBorderBrush x:Key="KilqpdiHbmgvaz" TargetTheme="Light" Color="#08000000" FallbackColor="{ThemeResource SystemAccentColor}"/>
这时编译直接说不可以使用
那么使用条件编译就可以让他编译通过
<contract5Present:RevealBorderBrush x:Key="KilqpdiHbmgvaz" TargetTheme="Light" Color="#08000000" FallbackColor="{ThemeResource SystemAccentColor}"/>
这时因为垃圾微软的 VisualStudio 的开发者不知道已经有这个,所以就告诉你错误,不要去理他,直接运行。
全部代码
<Page.Resources>
<contract5Present:RevealBorderBrush x:Key="KilqpdiHbmgvaz" TargetTheme="Light" Color="#08000000" FallbackColor="{ThemeResource SystemAccentColor}" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Margin="200,200,20,20"
contract5Present:Background="{StaticResource KilqpdiHbmgvaz}"
contract5NotPresent:Background="#FF565656" />
</Grid>
实际上就是 contract5Present 支持设置属性或控件,可以使用 contract5Present 用新的系统的控件。
所以也可以使用下面的方法,例如在 16299 才有的 ColorPicker ,如果希望程序在 15063 使用,在以前的系统使用 ComboBox ,那么就可以使用下面的代码
<contract5Present:ColorPicker /> <contract5NotPresent:ComboBox />
这样在新的系统就会使用 ColorPicker ,在以前的系统就会使用 ComboBox
如果在一个绑定一个使用了 contract5Present 的控件,那么在绑定的属性需要使用 contract5Present 不然微软的 VisualStudio 不然让你使用。
需要告诉大家,感觉说的 VisualStudio 在 Xaml 报告的错误,实际上这是Resharper的
如果觉得自己需要写的软件的版本比支持条件编译的版本还低,而且也不想写太多条件编译,请看使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题 - walterlv
参见
2019-5-21-win10-uwp-xaml-兼容多个版本条件编译的更多相关文章
- Win10 UWP xaml 延迟加载元素
xaml新增x:DeferLoadStrategy里面只有Lazy,查询了百度看到MSP_甄心cherish大神说的 xaml使用x:DeferLoadStrategy="Lazy" ...
- win10 uwp xaml 绑定接口
本文告诉大家如何在 xaml 绑定属性使用显式继承接口 早上快乐 就在你的心问了我一个问题,他使用的属性是显式继承,但是无法在xaml绑定 我写了简单的代码,一个接口和属性 public class ...
- win10 UWP 剪贴板 Clipboard
win10 UWP 剪贴板 Clipboard使用Windows.ApplicationModel.DataTransfer.Clipboard 设置文本 DataPackage dataPackag ...
- win10 uwp 读取保存WriteableBitmap 、BitmapImage
我们在UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap.关于 BitmapImage 和 WriteableBitmap 区别,我就不在这里说.主要说的 ...
- win10 uwp 手把手教你使用 asp dotnet core 做 cs 程序
本文是一个非常简单的博客,让大家知道如何使用 asp dot net core 做后台,使用 UWP 或 WPF 等做前台. 本文因为没有什么业务,也不想做管理系统,所以看到起来是很简单. Visua ...
- Win10 UWP开发系列:实现Master/Detail布局
在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...
- Win10 UWP开发实现Bing翻译
微软在WP上的发展从原来的Win7到Win8,Win8.1,到现在的Win10 UWP,什么是UWP,UWP即Windows 10 中的Universal Windows Platform简称.即Wi ...
- win10 uwp 列表模板选择器
本文主要讲ListView等列表可以根据内容不同,使用不同模板的列表模板选择器,DataTemplateSelector. 如果在 UWP 需要定义某些列的显示和其他列不同,或者某些行的显示和其他行不 ...
- win10 uwp 获得元素绝对坐标
有时候需要获得一个元素,相对窗口的坐标,在修改他的位置可以使用. 那么 UWP 如何获得元素坐标? 我提供了一个方法,可以获得元素的坐标. 首先需要获得元素,如果没有获得元素,那么如何得到他的坐标? ...
- UWP xaml 圆形头像
圆形头像 去掉黑边 拖动打开图形 圆形头像 现在很多软件都喜欢使用圆形头像 win10 uwp使用圆形头像很简单 <Ellipse Width="200" Height=&q ...
随机推荐
- 4、APP FPS测试
什么是FPS FPS是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数.FPS是测量用于保存.显示动态视频的信息数量.每秒钟帧数愈多,所显示的动作就会愈流畅.通常,要避免动作不 ...
- 洛谷 P3369 【模板】普通平衡树 (Treap)
题目链接:P3369 [模板]普通平衡树 题意 构造一种数据结构满足给出的 6 种操作. 思路 平衡树 平衡树的模板题. 先学习了一下 Treap. Treap 在插入结点时给该结点随机生成一个额外的 ...
- 1067 Sort with Swap(0, i) (25 分)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- 专题:OpenSSL
一.常用操作 对称加密: openssl enc -e -aes256 -base64 -in goal.file -out result.file 加密,-base64 指使用 base64 編码 ...
- leetcode-三角形的最大周长
Python解法: class Solution: def largestPerimeter(self, A: List[int]) -> int: A.sort() for i in rang ...
- Thinkphp 3.2 去掉index.php
1.httpd.conf中去掉LoadModule rewrite_module modules/mod_rewrite.so 前面的#号 2.httpd.conf 中 AllowOverride ...
- cut sort uniq wc 一 文本处理工具
cut cut是一个选取命令,就是将一段数据经过分析,取出我们想要的. 一般来说,选取信息通常是针对"行"来进行分析的,并不是整篇信息分析的. -c : 以字符为单位进行分割. c ...
- Android Activity XML配置
<activity android:name="xxxActivity" android:configChanges="keyboard|keyboardHidde ...
- mysql 删除同样记录只保留一条
delete from fa_order_account ) as a) ) as b)
- R语言 数据类型
R语言数据类型 通常,在使用任何编程语言进行编程时,您需要使用各种变量来存储各种信息. 变量只是保留值的存储位置. 这意味着,当你创建一个变量,你必须在内存中保留一些空间来存储它们. 您可能想存储各种 ...