尝试初始化变量,但我得到:System.NullReferenceException

时间:2016-01-11 07:07:15

标签: c# android mvvm xamarin model-view

我试图从View Model调用一个方法,但无论什么时候调用它,它都会执行初始化变量的第一个语句:System.NullReferenceException。

有人可以为此解释为什么我会收到此错误吗?如何解决它呢?谢谢。

型号:

namespace AppItem
{
    public class ItemOrder
    {
        public int item1 { get; set; }
        public int item2 { get; set; }
        public int item3 { get; set; }
    }
}

视图模型:

namespace AppItem
{
    public class ItemOrderViewModel : BaseViewModel
    {
        public Item item { get; set; }  

        // Initialize all items to zero
        public void initializeItems()
        {
            item.Item1 = 0; // System.NullReferenceException has been thrown Object reference not set to an instance of an object
            item.Item2 = 0;
            item.Item3 = 0;
        }

        public int addItemQty()
        {
            item.Item1++;

            return item.item1;
        }
    }
}

的活动:

namespace AppItem.Droid
{
    [Activity (Label = "ItemOrderActivity")]            
    public class ItemOrderActivity : BaseActivity <ItemOrderViewModel>
    {
        Button addItem;
        TextView itemQty;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.PlaceOrderLayout);
            addItem = FindViewById<Button> (Resource.Id.buttonAddItem);

            // Buttons
            addItemClick += OnAddItem;

            // Setting all garments to 0
            viewModel.initializeGarmentQuantity();

        }

        void OnAddItem (object sender, EventArgs e)
        {
            try
            {
                int i = viewModel.addItemQty();
                itemQty.Text = i.ToString(); 
            }
            catch (Exception exc) 
            {
                DisplayError (exc);
            }
        }
    }
}

基础活动:

namespace AppItem.Droid
{
    [Activity]          
    public class BaseActivity<TViewModel> : Activity
        where TViewModel : BaseViewModel
    {
        protected readonly TViewModel viewModel;
        //protected ProgressDialog progress;

        public BaseActivity()
        {
            viewModel = ServiceContainer.Resolve(typeof(TViewModel)) as TViewModel;
        }

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            //progress = new ProgressDialog(this);
            //progress.SetCancelable(false);
            //progress.SetTitle(Resource.String.Loading);
        }

        protected void DisplayError(Exception exc)
        {
            //Eror message here
        }
    }
}

服务容器:

namespace AppItem
{
    public static class ServiceContainer
    {
        static readonly Dictionary<Type, Lazy<object>> services = new Dictionary<Type, Lazy<object>>();

        public static void Register<T>(Func<T> function)
        {
            services[typeof(T)] = new Lazy<object>(() => function());
        }

        public static T Resolve<T>()
        {
            return (T)Resolve(typeof(T));
        }

        public static object Resolve(Type type)
        {
            Lazy<object> service;
            if (services.TryGetValue(type, out service))
            {
                return service.Value;
            }
            throw new Exception("Service not found!");
        }
    }
}

0 个答案:

没有答案