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 ...
随机推荐
- 如何使用Intellij IDEA工具导入SVN项目
Intellij IDEA是目前主流的IDE开发工具,工程项目导入也是必不可少的操作,本文讲述如何用 IDEA工具导入SVN项目. 步骤一:选择VCS打开Intellij IDEA开发工具,在导航栏中 ...
- PAT_A1070#Mooncake
Source: PAT A1070 Mooncake (25 分) Description: Mooncake is a Chinese bakery product traditionally ea ...
- upc组队赛2 Master of GCD 【线段树区间更新 || 差分】
Master of GCD 题目描述 Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Haka ...
- 剑指offer——570~n-1中缺失的数字
题目:0~n-1中缺失的数字. 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内. 在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字. ...
- 前端(十七)—— jQuery基础:jQuery的基本使用、JQ功能概括、JS对象与JQ对象转换、Ajax简单应用、轮播图
jQuery的基本使用.JQ功能概括.JS对象与JQ对象转换.Ajax简单应用.轮播图 一.认识jQuery 1.什么是jQuery jQuery是对原生JavaScript二次封装的工具函数集合 j ...
- Spring Boot主要目标
Spring Boot主要目标 Spring Boot的主要目标是: 为所有Spring开发提供一个基本的,更快,更广泛的入门体验. 开箱即用,但随着需求开始偏离默认值,快速启动. 提供大型项目(例如 ...
- C#比较两个日期的大小
DateTime dt1 = DateTime.Parse("2006-04-01"); DateTime dt2 = DateTime.Parse("2006-05-0 ...
- JSON.toJSONString()null值转“”
public static void main(String[] s) { CybWmsCommoditiesVo cybWmsCommoditiesVo = new CybWmsCommoditie ...
- Spring Boot Dubbo 应用启停源码分析
作者:张乎兴 来源:Dubbo官方博客 背景介绍 Dubbo Spring Boot 工程致力于简化 Dubbo | grep tid | grep -v "daemon" tid ...
- 在vue中运用mt-loadmore 实现上拉加载,下拉刷新
元旦了,给手残党直接复制的机会,代码如下: 1. :style="{'-webkit-overflow-scrolling': scrollMode}" 最外层div设置,以便兼容 ...