I have a problem with navigating with an MvvmCross navigation command which has a button binding. I'm currently testing my code on Android. I want to navigate on a button click, and using the
IMvxNavigationService
to
Navigate
to the next ViewModel, which I have set up a command and created a binding
It gives me the
Exception: Default [Error] MvxAsyncCommand : exception executing taskSystem.NullReferenceException: Object reference not set to an instance of an object.
It looks like its the
IMvxNavigationService Navigate
itself that is giving the NullReferenceException and that there's no problem the way I have set up the command.
Android logs:
pixel_2_q_10_0_-_api_29 Error 32148 Default [Error] MvxAsyncCommand : exception executing taskSystem.NullReferenceException: Object reference not set to an instance of an object.
at MvxFuelPriceCalculator.Core.ViewModels.FuelPriceCalcViewModel.<get_NavigateCommand>b__56_0 () [0x00000] in C:\Users\Work & Education\Documents\Programming\MvxFuelPriceCalculator\src\MvxFuelPriceCalculator.Core\ViewModels\FuelPriceCalcViewModel.cs:216
at MvvmCross.Commands.MvxAsyncCommandBase.ExecuteConcurrentAsync (System.Object parameter, System.Boolean hideCanceledException06-22 13:23:16.852 E/Default (32148): at MvvmCross.Commands.MvxAsyncCommand+<>c__DisplayClass2_0.<.ctor>b__0 (System.Threading.CancellationToken _) [0x00000] in /_/MvvmCross/Commands/MvxAsyncCommand.cs:190
at MvvmCross.Commands.MvxAsyncCommand.ExecuteAsyncImpl (System.Object parameter) [0x00000] in /_/MvvmCross/Commands/MvxAsyncCommand.cs:208
at MvvmCross.Commands.MvxAsyncCommandBase.ExecuteAsync (System.Object parameter, System.Boolean hideCanceledException) [0x00040] in /_/MvvmCross/Commands/MvxAsyncCommand.cs:88
at MvvmCross.Commands.MvxAsyncCommandBase.Execute (System.Object parameter) [0x00030] in /_/MvvmCross/Commands/MvxAsyncCommand.cs:70
FirstViewModel.cs
using System.Threading.Tasks;
using System.Windows.Input;
using MvvmCross.Commands;
using MvvmCross.ViewModels;
using MvxFuelPriceCalculator.Core.Services;
using MvvmCross.Navigation;
namespace MvxApplication.Core.ViewModels
{
public class FirstViewModel : MvxViewModel
{
public override async Task Initialize()
{
await base.Initialize();
}
private readonly IMvxNavigationService _navigationService;
public FirstViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
}
private IMvxAsyncCommand _navigateCommand;
public IMvxAsyncCommand NavigateCommand
{
get
{
_navigateCommand = _navigateCommand ??
new MvxAsyncCommand(() =>
_navigationService.Navigate<SecondViewModel>());
return _navigateCommand;
}
}
}
}
SecondViewModel.cs
using MvvmCross.ViewModels;
namespace MvxApplication.Core.ViewModels
{
public class SecondViewModel : MvxViewModel
{
}
}
FirstView.cs
using Android.Text;
using Android.Util;
using Android.Views;
using Android.Widget;
using MvvmCross.Platforms.Android.Views;
using MvxApplication.Core.ViewModels;
namespace MvxApplication.Droid.Views
{
[Activity(Label = "", MainLauncher = false)]
public class FirstView : MvxActivity<FirstViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.first_layout);
RelativeLayout relativeLayout = FindViewById<RelativeLayout>
(Resource.Id.relativeLayout);
}
}
}
first_layout.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/design_dark_default_color_background" >
<Button android:id="@+id/top_bar_btn"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:clickable="true"
local:MvxBind="Click NavigateCommand"/>
</RelativeLayout>
SecondView.cs
using Android.App;
using Android.OS;
using MvvmCross.Platforms.Android.Views;
using MvxApplication.Core.ViewModels;
namespace MvxApplication.Droid.Views
{
[Activity(Label = "Second View", MainLauncher = false)]
public class SecondView : MvxActivity<SecondViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.second_layout);
}
}
}
second_layout.axml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/design_dark_default_color_background" >
<ImageView
android:layout_width="match_parent"
android:layout_height="110dp"
android:src="@drawable/rectangle_light"/>
</RelativeLayout>
What I have tried:
I've followed the documentations, on
Navigation and
Extra - Navigation. And I've looked at some previous posts which haven't helped much.
I have checked the binding and also tried binding it programatically.
I also tried doing this from
this post
public class SecondViewModel : MvxViewModel<FirstViewModel>
Which just gives this error
Error CS0534 'SecondViewModel' does not implement inherited abstract member 'MvxViewModel<FirstViewModel>
Thanks for any help I can get