问题描述
所以我在这个 udemy 课程中,他们使用了下面一段已弃用的代码。我意识到 switch 或 if 语句可以解决这个问题,但我想看看除了 RuntimePlatform 之外是否还有其他东西可以用 switch 来代替它。毕竟我确实喜欢这个功能。
Device.OnPlatform<Thickness>(
iOS: new Thickness(0,20,0),Android: new Thickness(0,10,WinPhone: new Thickness(0,0));
诚然,它看起来不像,但我希望能有惊喜!
解决方法
很抱歉没有让您感到惊讶...正如您所知,这已经折旧了,我们的想法是使用返回 Device.RuntimePlatform
的 string
。
不,没有其他方法,除非您自己制作。如果你真的不喜欢 switch
语法,你可以使用 switch 表达式
var something = Device.RuntimePlatform switch
{
"iOS" => new Thickness(0,20,0),"Android" => new Thickness(0,10,"WinPhone" => new Thickness(0,_ => throw new ArgumentOutOfRangeException()
};