MonoGame移动跨平台项目在iOS上运行良好但在Android上引发错误?

时间:2016-07-03 19:15:24

标签: c# android ios xamarin monogame

我最近在MonoGame上为Android和iOS开发过一款游戏,它在iOS模拟器中运行良好,但是当我想在我的Android设备上运行它时,我得到了这3个错误

/Users/edward/Library/Mobile Documents/com~apple~CloudDocs/apps/Xamarin/Android/Radiant/Android/Activity1.cs(10,10): Error CS0272: The property or indexer `Microsoft.Xna.Framework.Game.Activity' cannot be used in this context because the set accessor is inaccessible (CS0272) (Radiant.Droid)

/Users/edward/Library/Mobile Documents/com~apple~CloudDocs/apps/Xamarin/Android/Radiant/Android/Activity1.cs(4,4): Error CS1502: The best overloaded method match for `Android.App.Activity.SetContentView(Android.Views.View)' has some invalid arguments (CS1502) (Radiant.Droid)

/Users/edward/Library/Mobile Documents/com~apple~CloudDocs/apps/Xamarin/Android/Radiant/Android/Activity1.cs(21,21): Error CS1503: Argument `#1' cannot convert `Microsoft.Xna.Framework.GameWindow' expression to type `Android.Views.View' (CS1503) (Radiant.Droid)

编辑:相关代码就在这里

public class Activity1 : AndroidGameActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Create our OpenGL view, and display it
        Game1.Activity = this;
        var g = new Game1();
        SetContentView(g.Window);
        g.Run();
    }

}

1 个答案:

答案 0 :(得分:1)

好的,我可以通过用此代码替换它来修复它

public class Activity1 : AndroidGameActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        var g = new Game1();
        SetContentView((View)g.Services.GetService(typeof(View)));
        g.Run();
    }
}