Contact Us
Magenic
  • What We Do
  • How We Do It
  • Our Thinking
  • Join Us
  • Our Work
  • Industries
  • Cloud
  • Software Development
  • Quality Engineering
  • DevOps
  • Strategy
  • Experience Design
  • Data & Integration
  • Advisory Services
  • Careers
  • Culture
  • Events
  • Success Stories
  • Partnerships
  • Technologies
  • Professional Services
  • Financial Services
  • Retail
  • Health Care

A Comprehensive Guide to Creating a Xamarin.Forms App with MvvmCross (Part 3 of 5)

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

  1. Add a Helpers folder
  2. Add the Helpers/IMvxPageNavigationProvider interface
    1. Here's the source I used: https://github.com/Cheesebaron/Xam.Forms.Mvx/blob/master/Movies/Movies.Android/MvxDroidAdaptation/IMvxPageNavigationHost.cs
public interface IMvxPageNavigationProvider
{
	void Push(Page page);
	void Pop();
}

 

  1. Add the Helpers/IMvxPageNavigationHost interface
    1. Here's the source I used: https://github.com/Cheesebaron/Xam.Forms.Mvx/blob/master/Movies/Movies.Android/MvxDroidAdaptation/IMvxPageNavigationHost.cs
public interface IMvxPageNavigationHost
{
	IMvxPageNavigationProvider NavigationProvider { get; set; }
}

 

  1. Add the Helpers/MvxPagePresenter class
    1. Here’s the source I used: https://github.com/Cheesebaron/Xam.Forms.Mvx/blob/master/Movies/Movies.Android/MvxDroidAdaptation/MvxPagePresenter.cs
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; }
}

 

  1. 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:

  1. 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:

  1. Add the Helpers/MvxNavigationActivity class
    1. 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; }
}

 

  1. 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.

Categories // Software Development
Tags Xamarin , Xamarin.Forms , MvvmCross , MVVM , Android , iOS , WindowsPhone
SHARE:
THE LATEST:
  • JANUARY 22, 2021 // blog
    Security In Five Bi-Weekly Roundup – 1/22/21
  • JANUARY 19, 2021 // blog
    Magenic has agreed to join Cognizant Softvision
  • JANUARY 8, 2021 // blog
    Security In Five Bi-Weekly Roundup – 1/8/21
Featured Content:
  • JANUARY 25, 2021 // White Paper
    The Top 7 Technology Trends of 2021

related Posts

JULY 19, 2017 // Software Development // Blog

What iOS 11 64-bit Support Means for Your Mobile App

FEBRUARY 25, 2016 // Software Development // Blog

Microsoft Buys Xamarin - Analysis

NOVEMBER 17, 2015 // Software Development // Blog

Magenic Named a Xamarin Elite Consulting Partner

JULY 17, 2015 // Software Development // Blog

A Comprehensive Guide to Creating a Xamarin.Forms App with MvvmCross (Part 5 of 5)

Ready to speak with our experts?

Have a question?

This field is required
This field is required
This field is required
This field is required

Thanks for Contacting Magenic

One of our experts will be contacting you directly within the next business day.

Return To Home
Magenic

info@magenic.com+1.877.277.1044

  • Facebook
  • Twitter
  • LinkedIn
  • YouTube
  • RSS Feed

© Magenic Inc.Privacy NoticeTerms & ConditionsSitemap