android - Device.OnPlatform 和 Switch() 之间的 Xamarin 应用混淆

标签 android xamarin xamarin.forms

我目前正在做一个实习项目,需要我使用一个过时的项目。 我仍在尝试构建项目,消除了所有错误,但现在这些过时行的警告不允许我通过。

我有以下错误:

Warning CS0618 'Device.OnPlatform(T, T, T)' is obsolete: 'OnPlatform<> (generic) is obsolete as of version 2.3.4. Please use switch(RuntimePlatform) instead.'

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace MobileMedico.Classes
{
    public static class BoolResources
    {
        public static readonly bool ShouldShowBoxView =  Device.OnPlatform(true, false, true);
    }
}

遗憾的是我不知道如何使用 Switch 方法。谁能帮帮我?

最佳答案

是的,该方法现在已过时。为了针对特定设备,您确实必须使用将运行时平台作为参数的 switch 语句。这是一个示例。

  static class AppConstants
{
    public static readonly Thickness PagePadding = GetPagePadding();

    private static Thickness GetPagePadding()
    {
        double topPadding;

        switch(Device.RuntimePlatform)
        {
            case Device.iOS:
                topPadding = 20;
                break;
            default:
                topPadding = 0;
                break;
        }

        return new Thickness(5, topPadding, 5, 0);
    }
}

关于android - Device.OnPlatform 和 Switch() 之间的 Xamarin 应用混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49497404/

相关文章:

android - 如何为 NDK 静态库编写/调试 Android.mk?

android - 为什么使用 Messenger 而不是将引用传递给 Handler?

xamarin - 如何在 Xamarin.Forms 中检测设备的屏幕方向?

javascript - Syncfusion 控件未在 iOS native UIWebView 上加载

c# - Xamarin Forms 图像未显示在 ListView 中

c# - 在 Xamarin forms pcl 项目中使用 HttpUtility

android - 方向改变后 MenuItem alpha 值丢失

android - ListView 多选 - 默认透明颜色不能正常工作?

ios - 可以仅使用 Windows PC 使用 Visual Studio 模拟 iOS 应用程序

xamarin - 如何使用TabbedPage.ToolbarPlacement ="Bottom"-Xamarin Forms实现TabLayout.IOnTabSelectedListener.OnTabUnselected?