不推荐使用 Device.OnPlatform [英] Device.OnPlatform deprecated

查看:29
本文介绍了不推荐使用 Device.OnPlatform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 ContentPage 的构造函数中,我尝试设置一个平台相关的填充值:

Inside the constructor of my ContentPage I try to set a platform dependent padding value:

Padding = new Thickness(5, Device.OnPlatform(20, 5, 5), 5, 5);

Visual Studio 在 Device.OnPlatform 下划线,当我将鼠标指针悬停在方法调用上时,我收到以下警告:

Visual Studio underlines Device.OnPlatform and when I hover the mouse pointer over the method call I get the following warning:

Devide.OnPlatform(T, T, T) 已过时:'使用switch(RuntimePlatform) 代替'.

Devide.OnPlatform(T, T, T) is obsolete: 'Use switch(RuntimePlatform) instead'.

最初使用的代码来自电子书 2016 年出版的使用 Xamarin.Forms 创建移动应用书".我真的很惊讶这个平台的发展速度如此之快!

The code initially used is from the e-book 'Creating Mobile Apps with Xamarin.Forms Book' published in 2016. I 'm really surprised how fast this platform evolves!

不幸的是,我不知道如何使用警告建议的方式替换 Device.OnPlatform.

Unfortunately I'm not aware of how Device.OnPlatform should be replaced using the way suggested by the warning.

推荐答案

2016 年该方法被弃用.

2016 was the year this method became deprecated.

您应该使用 switch 语句来确定操作系统.

You're supposed to use a switch statement to determine the OS.

switch(Device.RuntimePlatform)
{
    case Device.iOS:
      return new Thickness(5, 5, 5, 0)
    default:
      return new Thickness(5, 5, 5, 0)
 }

您当然可以将其包装在一个函数中,该函数将执行与您希望使用 Device.OnPlatform 相同的工作,但您将调用您自己的函数,而不是调用 Device.OnPlatform.

You can of course wrap this inside a function which will do the same job as you wished to do with Device.OnPlatform, but instead of calling Device.OnPlatform you'll call your own function.

这篇关于不推荐使用 Device.OnPlatform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆