无法确定 Xamarin Forms 应用程序是 运行 在模拟器中还是在设备上

Unable to determine if the Xamarin Forms application is running in the Simulator or on the Device

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

各种回答都在建议我做以下事情:

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

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

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

使用子句:

using ObjCRuntime;

代码:

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

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

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

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

源自 this answer