【问题标题】:Unable to determine if the Xamarin Forms application is running in the Simulator or on the Device无法确定 Xamarin Forms 应用程序是在模拟器中还是在设备上运行
【发布时间】:2016-05-01 07:04:15
【问题描述】:

我正在尝试确定应用是在模拟器中运行还是在硬件 (Apple iPhone) 设备上运行。

各种答案都在暗示我做以下事情:

bool isSimulator = MonoTouch.ObjCRuntime.Runtime.Arch ==
    MonoTouch.ObjCRuntime.Arch.SIMULATOR;

我已将其添加到我的 iOS 应用程序 AppDelegate.cs 文件中。但它确实可以编译 - 我缺少命名空间或程序集。

这是 FULL 方法的图片(颜色编码显示它找不到静态属性):

【问题讨论】:

    标签: c# ios xamarin xamarin.forms


    【解决方案1】:

    使用子句:

    using ObjCRuntime;
    

    代码:

    bool isSimulator = Runtime.Arch == Arch.SIMULATOR;
    

    仅供参考:MonoTouch 命名空间已弃用(~2012 年),并已在“Xamarin.iOS”产品中分解为多个命名空间。

    【讨论】:

    • 如果您在代码中指定它,您实际上并不需要 using ObjCRuntime。不再需要对 MonoTouch 命名空间进行更正。
    【解决方案2】:

    使用UIDevice.CurrentDevice 信息的替代方法:

    public static class IosHelper
    {
        public static bool IsSimulator {
            get {
                return UIDevice.CurrentDevice.Model.EndsWith("Simulator") || UIDevice.CurrentDevice.Name.EndsWith("Simulator");
            }
        }
    }
    

    源自this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 2020-02-17
      • 2018-02-17
      相关资源
      最近更新 更多