Being new to MvvmCross I've decided to create a small Xamarin.Forms app. I have a MainPage.xaml
tied to its ViewModel MainViewModel.cs
which is displayed first. I have a FirstView.axml
located in the droid project along with its activity. The associated ViewModel is located in the Core project along side the MainViewModel and is named FirstViewModel.cs
Upon clicking a navigate button I want MvvmCross to display the FirstView.axml
layout and bind to the VM. However,
Whenever the command is invoked I get
03-10 10:11:38.704 D/ViewRootImpl(18964): ViewPostImeInputStage ACTION_DOWN
mvx:Diagnostic: 17,87 Showing ViewModel FirstViewModel
03-10 10:11:38.854 I/mono-stdout(18964): mvx:Diagnostic: 17,87 Showing ViewModel FirstViewModel
[0:] mvx:Diagnostic: 17,87 Showing ViewModel FirstViewModel
mvx:Error: 17,91 Failed to create ContentPage FirstPage
03-10 10:11:38.894 I/mono-stdout(18964): mvx:Error: 17,91 Failed to create ContentPage FirstPage
[0:] mvx:Error: 17,91 Failed to create ContentPage FirstPage
mvx:Error: 17,92 Skipping request for FirstViewModel
03-10 10:11:38.904 I/mono-stdout(18964): mvx:Error: 17,92 Skipping request for FirstViewModel
[0:] mvx:Error: 17,92 Skipping request for FirstViewModel`
Currently the project looks like this:
Startup Activity
[Activity(Label = "Hello MvvmCrossForms", MainLauncher = true)]
public class CrossFormsApp : FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//Init forms
Forms.Init(this, bundle);
InitialiseMvx();
//Create mvxformsApp
var mvxFormsApp = new MvxFormsApp();
var presenter = Mvx.Resolve<IMvxViewPresenter>() as MvxFormsDroidPagePresenter;
//Assign the viewPresenter
presenter.MvxFormsApp = mvxFormsApp;
LoadApplication(mvxFormsApp);
//Start mvxApp
Mvx.Resolve<IMvxAppStart>().Start();
}
private void InitialiseMvx()
{
if (MvxSingleton<IMvxIoCProvider>.Instance == null)
Mvx.RegisterSingleton(MvxSimpleIoCContainer.Initialize());
MvxAndroidSetupSingleton.EnsureSingletonAvailable(this.ApplicationContext)
.EnsureInitialized();
}
}
MainViewModel
public class MainViewModel : MvxViewModel
{
private string _inputString;
public ICommand NavigateCommand
{
get { return new MvxCommand(() => ShowViewModel<FirstViewModel>()); }
}
public string InputString
{
get { return _inputString; }
set { SetProperty(ref _inputString, value); }
}
}
Essentially what I'm looking for is the reverse of this: MvvmCross: How to navigate from regular view to Mvvm viewmodel on Android?
What I ended up doing was defining a empty interface
IXFViewModel
which I implement on theForms
ViewModels. Then in my custom presenter I get a list of interfaces which theMvxViewModelRequest.ViewModelType
implements.If the type
IXFViewModel
is found in the list of interfaces, the custom presenter - which extendsMvxFormsDroidPagePresenter
- handles the presentation by callingbase.Show(request)
. If theIXFViewModel
type is not found the custom presenter switches to anMvxAndroidViewPresenter
to handle the view presentation.The solution below:
Bootstrap
The Custom Presenter
The Launcher and Forms Activity
The Interface and ViewModel