July 1, 2015 // By Brent Edwards
This is the third part in a 5 part series. In the first part, we looked at creating a Xamarin.Forms app and why you would want to use MvvmCross with it. In the second part, we looked at adding MvvmCross to your Xamarin.Forms app. In this post, we will get the app running on the Android platform.
We’re going to dive right into the code, where we left off.
In the Android project
- Add a Helpers folder
- Add the Helpers/IMvxPageNavigationProvider interface
public interface IMvxPageNavigationProvider { void Push(Page page); void Pop(); }
- Add the Helpers/IMvxPageNavigationHost interface
public interface IMvxPageNavigationHost { IMvxPageNavigationProvider NavigationProvider { get; set; } }
- Add the Helpers/MvxPagePresenter class
public sealed class MvxPagePresenter : MvxAndroidViewPresenter, IMvxPageNavigationHost { public override void Show(MvxViewModelRequest request) { if (TryShowPage(request)) return; Mvx.Error("Skipping request for {0}", request.ViewModelType.Name); } private bool TryShowPage(MvxViewModelRequest request) { if (this.NavigationProvider == null) return false; var page = MvxPresenterHelpers.CreatePage<Page>(request); if (page == null) return false; var viewModel = MvxPresenterHelpers.LoadViewModel(request); page.BindingContext = viewModel; this.NavigationProvider.Push(page); return true; } public override void Close(IMvxViewModel viewModel) { if (this.NavigationProvider == null) return; this.NavigationProvider.Pop(); } public IMvxPageNavigationProvider NavigationProvider { get; set; } }
- Update Setup to override CreateViewPresenter method and return the newly created presenter
protected override IMvxAndroidViewPresenter CreateViewPresenter() { var presenter = new MvxPagePresenter(); Mvx.RegisterSingleton<IMvxPageNavigationHost>(presenter); return presenter; }
In the Portable or Shared project:
- Add the Helpers/IUiContext interface and the Helpers/UiContext class
Portable:
public interface IUiContext { object CurrentContext { get; set; } }
Shared:
public interface IUiContext { #if ANDROID Android.App.Activity CurrentContext { get; set; } #elif IOS UIKit.UIViewController CurrentContext { get; set; } #endif }
Portable:
public class UiContext : IUiContext { public object CurrentContext { get; set; } }
Shared:
public class UiContext : IUiContext { #if ANDROID public Android.App.Activity CurrentContext { get; set; } #elif IOS public UIKit.UIViewController CurrentContext { get; set; } #endif }
In the Android project:
- Add the Helpers/MvxNavigationActivity class
- This is a good starting point: https://github.com/Cheesebaron/Xam.Forms.Mvx/blob/master/Movies/Movies.Android/MvxDroidAdaptation/MvxNavigationActivity.cs
[Activity(Label = "XFormsCross Template", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MvxNavigationActivity : FormsApplicationActivity, IMvxPageNavigationProvider { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); Xamarin.Forms.Forms.Init(this, bundle); var uiContext = new UiContext { CurrentContext = this }; Mvx.Resolve<IMvxPageNavigationHost>().NavigationProvider = this; Mvx.Resolve<IMvxAppStart>().Start(); } public async void Push(Page page) { if (MvxNavigationActivity.NavigationPage != null) { await MvxNavigationActivity.NavigationPage.PushAsync(page); return; } MvxNavigationActivity.NavigationPage = new NavigationPage(page); this.SetPage(MvxNavigationActivity.NavigationPage); } public async void Pop() { if (MvxNavigationActivity.NavigationPage == null) return; await MvxNavigationActivity.NavigationPage.PopAsync(); } public static NavigationPage NavigationPage { get; set; } }
- Change SplashScreen to override InitializationComplete and add:
public override void InitializationComplete() { StartActivity(typeof(MvxNavigationActivity)); }
Good news! You should now have your Android app up and running. I say should because there are a couple of gotchas you may have run into.
First, with the Shared project type, there may be some iOS code that doesn’t compile. It should be safe to comment that out for the moment. We’ll come back to it later.
Second, with the WinPhone project, there may be some compiler errors around string resources. This is likely due to the fact that we removed WinPhone from the namespace declaration. Make sure the using statements have the updated namespace.
In the next part, we’ll look at getting the app up and running for WinPhone.
You’ve just read part 3 of the “Comprehensive Guide to Creating a Xamarin.Forms App with MvvmCross” blog series. To read part 4, click here. If you’d like to contact Magenic, email us or call us at 877-277-1044.