c# - 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 您必须调用 Xamarin.Forms.Init(); 在使用它之前。
我的代码有什么问题?我不知道我应该添加或更改什么。
代码(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);
}
}
}
代码(程序.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();
}
}
}
解决方案
The Device
class is part of Xamarin Forms. If you are using Xamarin Forms, then you must initialize it first. That is exactly what the error message is telling you.
推荐阅读
- c# - OnTriggerStay 仅适用于一个对象
- csv - csv 标头之前的东西,当我在 rust 中使用 serde 时如何处理它
- elasticsearch - 如何使用 Amazon AMI 设置多节点 Elasticsearch 集群
- python-3.x - 如何使python中的大型复制操作更高效
- c - 如何在C中存储多个二维数组位置?
- java - 如何使用 mockito 进入模拟对象的箭头方法调用
- php - 安装 symfony 框架
- javascript - 如何从数组中想要形成一个模式来进行查询以进行 API 调用
- python - 为什么在处理数字时该功能无法正常工作?
- mysql - Join + group by,然后过滤包含“x”而不是“y”的组行