未处理的异常:System.TypeInitializationException:类型初始值设定项引发了异常

时间:2019-07-10 20:04:29

标签: c# xamarin xamarin.forms xamarin.android

我正在创建自己的第一个基本的基于文​​本的移动rpg游戏,以熟悉Xamarian.Forms并了解有关C#的更多信息。我已经设置了菜单和基本主页,以便可以将游戏逻辑中的数据绑定到一些标签和按钮上,以检查逻辑代码是否正常工作。

我已经将数据和方法绑定到标签和按钮上,因此我希望主页显示出来。但是,每次我尝试调试时都会崩溃,并显示以下错误消息:

  

未处理的异常:

     

System.TypeInitializationException:类型初始化器   “ Engine.World”引发了异常。

我查看了各个异常以更好地理解问题,并说我的World类中LocationText,Locations和Player1的值都设置为null。但是我很确定我已经声明和/或初始化了这些值。

这是我的World.cs课:

using System;
using System.Collections.Generic;
using System.Text;

namespace Engine
{
    public static class World
    {
        public static decimal TrueDay = 1;
        public static string LocationText = World.Player1.CurrentLocation.Name.ToString();

        public static Player Player1;

        public static readonly List<Location> Locations = new List<Location>();

        public const int LOCATION_ID_OSHAWA = 1;
        public const int LOCATION_ID_TORONTO = 2;

        static World()
        {
            PopulateLocations();            
        }

        private static void PopulateLocations()
        {
            Location oshawa = new Location(LOCATION_ID_OSHAWA, "Oshawa");
            Location toronto = new Location(LOCATION_ID_TORONTO, "Toronto");

            Locations.Add(oshawa);
            Locations.Add(toronto);
        }

        public static Location LocationByID(int id)
        {
            foreach (Location location in Locations)
            {
                if (location.ID == id)
                {
                    return location;
                }
            }

            return null;
        }
    }
}

这是我的App.xaml.cs,其中我从Player1类初始化World

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Engine;

namespace TestApp
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            //this is where exception is thrown
            World.Player1 = new Player("Jordan", World.LocationByID(World.LOCATION_ID_OSHAWA), 5000);

            MainPage = new KingPinMasterDetailPage();


        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

这是我的HomePageViewModel.cs,如果它恰好与之相关。我注意到有两次,当我将属性更改为静态或从静态更改为试图解决该问题时,会引发相同的异常,即主页的绑定上下文为空,而不是World中的属性和变量。课。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using Engine;

namespace TestApp
{
    public class HomePageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public static Player _player = World.Player1;

        public decimal Day = World.TrueDay;
        public decimal CurrentDay
        {

            get { return Day; }

            set { Day = value; OnPropertyChanged(); }
        }

        public decimal MoneyValue = Convert.ToInt32(Math.Floor(World.Player1.PlayerMoney));
        public decimal Money
        {

            get { return MoneyValue; }

            set { MoneyValue = value; OnPropertyChanged(); }
        }

        public string CurrentLocation = _player.CurrentLocation.Name;
        public string PlayerLocation
        {

            get { return CurrentLocation; }

            set { CurrentLocation = value; OnPropertyChanged(); }
        }

        public void Onturn()
        {

            World.TrueDay = World.TrueDay + 1;

            World.Player1.PlayerMoney = World.Player1.PlayerMoney + 1000;
        }

        void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}

这是抛出异常的地方

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TestApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : ContentPage
    {
        public HomePage()
        {
            InitializeComponent();

            //this is where the other exception would throw
            BindingContext = new HomePageViewModel();
        }
    }
}

我希望我提供了所有相关信息。我已经花了几天时间浏览整个互联网以及该站点上的其他类似帖子,试图解决此问题,以便我可以继续使用我的应用程序。就目前而言,我基本上处于停滞状态。我知道它可能摆在我面前,例如错误的变量是静态的或简单的东西,但是由于某种原因,我只是没有看到它。任何帮助将不胜感激。谢谢您的宝贵时间!

0 个答案:

没有答案