Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin中的意图未传递正确的值_C#_Android_Visual Studio_Android Intent_Xamarin - Fatal编程技术网

C# Xamarin中的意图未传递正确的值

C# Xamarin中的意图未传递正确的值,c#,android,visual-studio,android-intent,xamarin,C#,Android,Visual Studio,Android Intent,Xamarin,我目前正在使用VisualStudio中的Xamarin为android开发一个小游戏,我有游戏活动和屏幕上的游戏。在“游戏结束”屏幕上,我希望显示用户得分,这是我从意图中获得的。这在第一次运行时效果很好,但是如果用户选择重新启动游戏,无论他们得分多少,屏幕上的游戏默认他们的得分为零。我如何解决这个问题,以便每次都显示正确的分数?另外,我对这一点还是相当陌生的,所以任何帮助都将是巨大的 游戏类中的代码通过屏幕调用游戏 this.intent = new Intent(Android.App.Ap

我目前正在使用VisualStudio中的Xamarin为android开发一个小游戏,我有游戏活动和屏幕上的游戏。在“游戏结束”屏幕上,我希望显示用户得分,这是我从意图中获得的。这在第一次运行时效果很好,但是如果用户选择重新启动游戏,无论他们得分多少,屏幕上的游戏默认他们的得分为零。我如何解决这个问题,以便每次都显示正确的分数?另外,我对这一点还是相当陌生的,所以任何帮助都将是巨大的

游戏类中的代码通过屏幕调用游戏

this.intent = new Intent(Android.App.Application.Context, typeof(GameOverAct));
                this.intent.PutExtra("ThePoint", buttons.score);
                this.intent.SetFlags(ActivityFlags.NewTask);
                Android.App.Application.Context.StartActivity(intent);
游戏结束活动

public class GameOverAct : Microsoft.Xna.Framework.AndroidGameActivity
{
    TextView score;
    protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);
        SetContentView(Resource.Layout.layout1);
        score = FindViewById<TextView>(Resource.Id.scoretext);
        score.Text = "Score: " + getGameScore();
        removescore();

        Button restart = FindViewById<Button>(Resource.Id.restart);
        restart.Click += (sender, e) =>
        {
            var intent = new Intent(this, typeof(Activity1));
            StartActivity(intent);
        };
        Button mm = FindViewById<Button>(Resource.Id.mainmenu);
        mm.Click += (sender, e) =>
        {
            var intent3 = new Intent(this, typeof(MainMenuAct));
            StartActivity(intent3);
        };
    }
    protected override void OnRestart()
    {
        base.OnRestart();
        SetContentView(Resource.Layout.layout1);
        score = FindViewById<TextView>(Resource.Id.scoretext);
        score.Text = "Score: " + getGameScore();
        removescore();
        Button restart = FindViewById<Button>(Resource.Id.restart);
        restart.Click += (sender, e) =>
        {
            var intent = new Intent(this, typeof(Activity1));
            StartActivity(intent);
        };
        Button mm = FindViewById<Button>(Resource.Id.mainmenu);
        mm.Click += (sender, e) =>
        {
            var intent3 = new Intent(this, typeof(MainMenuAct));
            StartActivity(intent3);
        };
    }
    public int getGameScore()
    {
        int a = Intent.GetIntExtra("ThePoint", 0);
        return a;
    }
    public void removescore()
    {
        Intent.RemoveExtra("ThePoint");
    }
 }

您的
按钮.score
是int类型的值吗?我无法根据您发布的代码复制您的问题。是的,这是一个intCan,请提供一个最小的可复制演示?不管我解决了它,我只是创建了一个新项目,并将我的代码复制到该项目上,它工作了
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
    Game1 g;

    //When the Game is played for the first time after launching the app, this method is called
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        //Gets a Game1 object, which is the class that builds the game
        this.g = new Game1();
        //sets it to view
        SetContentView((View)g.Services.GetService(typeof(View)));
        g.Run();


    }
    //If the player chooses to restart, this method is called
    protected override void OnRestart()
    {
        base.OnRestart();
        this.g = new Game1();
        SetContentView((View)g.Services.GetService(typeof(View)));
        g.Run();

    }
}