c# - Android MonoGame 游戏无法在手机上启动
问题描述
我正在学习适用于 Android 的 Xamarin 和 MonoGame。我有一个问题。在Android 9游戏模拟器上正常启动。在真手机上,游戏无法启动。我在装有 Android 10 和 Android 11 的手机上进行测试。我设置了所有 Android API 版本,但这无济于事。 Android 11 手机 Android 10 手机
文件“Game1.cs”的代码:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
namespace AndroidTest
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private SpriteFont font;
private Vector2 position;
private float x, y;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
position = new Vector2(10, 10);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
font = this.Content.Load<SpriteFont>("File");
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
TouchCollection tc = TouchPanel.GetState();
foreach (TouchLocation tl in tc)
{
position = tl.Position;
x = tl.Position.X;
y = tl.Position.Y;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
_spriteBatch.DrawString(font, "This is test!", position, Color.GreenYellow);
_spriteBatch.DrawString(font, string.Format("X:{0} Y:{1}", x, y), new Vector2(10, 1635), Color.Beige);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
文件“AndroidManifest.xml”的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AndroidTest.AndroidTest" android:versionCode="1" android:versionName="1.1" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="30" android:targetSdkVersion="30" />
<uses-permission android:name="android.permission.SET_ORIENTATION" />
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<application android:label="AndroidTest"></application>
</manifest>
文件“Activity1.cs”的代码
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Views;
using Microsoft.Xna.Framework;
namespace AndroidTest
{
[Activity(
Label = "@string/app_name",
MainLauncher = true,
Icon = "@drawable/icon",
AlwaysRetainTaskState = true,
LaunchMode = LaunchMode.SingleInstance,
ScreenOrientation = ScreenOrientation.FullUser,
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize
)]
public class Activity1 : AndroidGameActivity
{
private Game1 _game;
private View _view;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_game = new Game1();
_view = _game.Services.GetService(typeof(View)) as View;
SetContentView(_view);
_game.Run();
}
}
}
解决方案
推荐阅读
- c# - 双击列表框项目 - C#
- sql - 如何在没有临时表的情况下使用 SQL 游标跳过一行
- python - 异步 Python 和锁
- c# - Firebase-Admin 保存用户的自定义 userPropery
- c# - Microsoft.JSInterop.JSException:找不到(DxBlazor.Input.loadModule)
- python - 在使用 lru_cache 装饰器时将不同的参数模式视为相同的调用
- php - 使用 php / jquery 最好地返回 Hubspot 错误
- node.js - 如何使用节点 js 在 mongo db 中查找 db
- github - Terraform:是否可以连接多个 github 组织?
- angular - 无法将变量从一个组件传递到另一个组件 - 已关闭