Xamarin 工作室:System.InvalidOperationException

Xamarin Studio: System.InvalidOperationException

我在 Xamarin Studio 中创建了一个新的 MonoGame iOS 解决方案。之后,我将以下代码添加到 Game1.cs:

bool IsiOS = false;    
if (Device.RuntimePlatform == Device.iOS)
   IsiOS = true;

但是我在构建项目时收到错误消息。

if (Device.RuntimePlatform == Device.iOS)

System.InvalidOperationException You MUST call Xamarin.Forms.Init(); prior to using it.

我的代码有什么问题?我不知道我应该添加或更改什么。

代码(Game1.cs):

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Xamarin.Forms;

namespace NewTest.iOS
{
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    bool IsiOS = false;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.IsFullScreen = true;
    }

    protected override void Initialize()
    {  
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        if (Device.RuntimePlatform == Device.iOS)
            IsiOS = true;
    }

    protected override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);

        base.Draw(gameTime);
    }
}
}

代码(Program.cs):

using System;
using Foundation;
using UIKit;

namespace NewTest.iOS
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
    private static Game1 game;

    internal static void RunGame()
    {
        game = new Game1();
        game.Run();
    }

    static void Main(string[] args)
    {
        UIApplication.Main(args, null, "AppDelegate");
    }

    public override void FinishedLaunching(UIApplication app)
    {
        RunGame();
    }
}
}

Device class 是 Xamarin Forms 的一部分。如果您使用的是 Xamarin Forms,则必须先对其进行初始化。这正是错误消息告诉您的内容。