0
votes

I'm trying to create a Portable Class such that I can use that across the platforms. It is working fine in Windows Phone 8.1 App. But when it comes to Android, then it is showing the Viewmodel as null and DataContext as Null in debugger which breaks the application debugger. When I create another viewmodel and view to test the app, its working fine on android too. What can be the possible reasons.

EDIT : It is crashing due to the constructor , in which I am passing the business Logic instance. So , Constructor is necessary i think but in that case it is crashing.I am not trying to resolve the ViewModel , i am trying to resolve the Service instance in ViewModel and for the purpose of MVVM, I am keeping the Service out from Droid Project so base.OnCreate(bundle) does not come into scene anyways.

public BookViewModel(ILogic _logic) {

       logic = _logic;
       //var ss= Mvx.Resolve<ILogic>();
      //var  x = Mvx.CanResolve<ILogic>();
       _details = logic.Read();

   }

Below is the Logic Code :

 public class Logic : ILogic
{

    #region Attributes

    List<Detail.Detail> _details = new List<Detail.Detail>();
    DataLayer.DataLayer dl = new DataLayer.DataLayer();

    #endregion

    #region .ctor

    public Logic()
    {
        populateList();
    }

    #endregion

    #region Methods


   private void populateList()
    {
        _details = dl.Access();
    }

Below is the App.cs in ViewModel in which CanResolve is giving False

public class App : Cirrious.MvvmCross.ViewModels.MvxApplication {

    #region Methods
    public override void Initialize()
    {
        Mvx.RegisterType<ILogic, Logic>();
        var ss = Mvx.CanResolve<ILogic>();

        RegisterAppStart<ViewModels.BookViewModel>();
    }

    #endregion
}
1

1 Answers

0
votes

There are a few questions and answers around similar to this - e.g. similar to MVVMCross ViewModel construction failure notifications

The basic answer is that MvvmCross cannot resolve the ViewModel during the constructor - you have to wait until after the base.OnCreate(bundle) call - at this point the ViewModel will be resolved.

There's also a bit more about when ViewModel's are located in Who should create view model instances in MvvmCross and CoreDispatcher.HasThreadAccess "breaking change" (and probably a few other places too)