【问题标题】:Device.OnPlatform deprecatedDevice.OnPlatform 已弃用
【发布时间】:2017-08-05 20:35:08
【问题描述】:

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

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

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

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

最初使用的代码来自于 2016 年出版的 e-book 'Creating Mobile Apps with Xamarin.Forms Book'。我真的很惊讶这个平台的发展速度如此之快!

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

【问题讨论】:

标签: c# xamarin


【解决方案1】:

2016 年是这种方法被弃用的一年。

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

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。

【讨论】:

  • 以这种方式做同样的事情需要更多的代码,但我知道当添加新的设备类型时,更新的方法更容易扩展。
  • 不需要break 语句,因为它们永远不会被执行。
  • @Encrypt0r 没错,我在编写基本 switch 语句后添加了返回。编辑了我的答案。谢谢。
  • 你在我发布我的答案一天后编辑了这个答案..停止复制粘贴
  • @Rakesh 什么?您于 2017 年 9 月发布了您的...我的答案的最后一次编辑是 2017 年 8 月 6 日.. 不知道 9 月在 8 月之前是怎样的,因为我不是时间旅行者...我也不确定我能复制什么从你的回答..
【解决方案2】:
switch (Device.RuntimePlatform)
        {
            case Device.iOS:
                    Padding = new Thickness(5, 5, 5, 0);
                    break;

            default:
                    Padding = new Thickness(5, 5, 5, 0);
                    break;
         }

【讨论】:

    【解决方案3】:

    如果有人在 XAML 文件中遇到同样的问题,这是绕过已弃用消息的方法:

    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness">
            <On Platform="iOs">0,20,0,0</On>
        </OnPlatform>
    </ContentPage.Padding>
    

    【讨论】:

      【解决方案4】:

      我通过使用内联三元条件运算符解决了这个问题,这假设您只希望 iOS 有一些不同的东西

      Padding = new Thickness(5, (Device.RuntimePlatform==Device.iOS?20:5), 5, 5);
      

      【讨论】:

        【解决方案5】:

        这是一个令人难以置信的 API,我不认为 this forum 中所述的原因证明它的弃用是合理的。

        它可以很容易地扩展为支持多个平台。我了解there have 并且在不久的将来会有很多变化,但我认为任何开发人员都不会选择在每次必须做出简单决定时编写繁琐的switch 语句。这样的代码会变得很庞大。

        所以,我尝试重新创建Device.OnPlatform&lt;T&gt;

        namespace Xamarin.Forms
        {
            public static class DevicePlatform
            {
                private static T OnImpl<T>(T @default = default, T iOS = default, T Android = default, Func<T> computeCustom = null)
                {
                    switch (Device.RuntimePlatform)
                    {
                        case Device.iOS: return iOS;
        
                        case Device.Android: return Android;
        
                        default:
                            if (computeCustom == null) return @default;
                            return computeCustom.Invoke();
                    }
                }
        
                public static T On<T>(T @default = default, T iOS = default, T Android = default) => OnImpl(@default, iOS, Android, null);
        
                public static T On<T>(T @default = default, T iOS = default, T Android = default, (string Platform, T Value) custom = default)
                {
                    string runtimePlatform = Device.RuntimePlatform;
        
                    return OnImpl(@default, iOS, Android, () =>
                    {
                        if (custom.Platform == runtimePlatform)
                            return custom.Value;
        
                        return @default;
                    });
                }
        
                public static T On<T>(T @default = default, T iOS = default, T Android = default, (string Platform, T Value) custom = default, (string Platform, T Value) custom2 = default)
                {
                    string runtimePlatform = Device.RuntimePlatform;
        
                    return OnImpl(@default, iOS, Android, () =>
                    {
                        if (custom.Platform == runtimePlatform)
                            return custom.Value;
                        else if (custom2.Platform == runtimePlatform)
                            return custom2.Value;
        
                        return @default;
                    });
                }
        
                public static T On<T>(T @default = default, T iOS = default, T Android = default, (string Platform, T Value) custom = default, (string Platform, T Value) custom2 = default, (string Platform, T Value) custom3 = default)
                {
                    string runtimePlatform = Device.RuntimePlatform;
        
                    return OnImpl(@default, iOS, Android, () =>
                    {
                        if (custom.Platform == runtimePlatform)
                            return custom.Value;
                        else if (custom2.Platform == runtimePlatform)
                            return custom2.Value;
                        else if (custom3.Platform == runtimePlatform)
                            return custom3.Value;
        
                        return @default;
                    });
                }
        
                public static T On<T>(T @default = default, T iOS = default, T Android = default, (string Platform, T Value) custom = default, (string Platform, T Value) custom2 = default, (string Platform, T Value) custom3 = default, (string Platform, T Value) custom4 = default)
                {
                    string runtimePlatform = Device.RuntimePlatform;
        
                    return OnImpl(@default, iOS, Android, () =>
                    {
                        if (custom.Platform == runtimePlatform)
                            return custom.Value;
                        else if (custom2.Platform == runtimePlatform)
                            return custom2.Value;
                        else if (custom3.Platform == runtimePlatform)
                            return custom3.Value;
                        else if (custom4.Platform == runtimePlatform)
                            return custom4.Value;
        
                        return @default;
                    });
                }
        
                public static T On<T>(T @default = default, T iOS = default, T Android = default, params (string Platform, T Value)[] customPlatforms)
                {
                    string runtimePlatform = Device.RuntimePlatform;
        
                    return OnImpl(@default, iOS, Android, () =>
                    {
                        for (int i = 0; i < customPlatforms.Length; i++)
                        {
                            var platform = customPlatforms[i];
                            if (platform.Platform == runtimePlatform)
                                return platform.Value;
                        }
        
                        return @default;
                    });
                }
            }
        }
        
        

        这是一个如何使用它的示例:

        Padding = new Thickness(0, DevicePlatform.On(0, iOS: 20, Android: 0, custom: (Device.Tizen, 34)));
        

        如您所见,我们很酷的 API 又回来了,Tizen 将永远被人们铭记。

        我计划用它制作一个微型 nuget 包,因此我愿意接受任何形式的更正和帮助。

        干杯

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-02-12
          • 1970-01-01
          • 2016-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-20
          相关资源
          最近更新 更多