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
Magenic has agreed to join Cognizant Softvision to broaden our best-in-class digital product engineering services Magenic has agreed to join Cognizant Softvision Read the Release

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

July 13, 2015 // By Brent Edwards

This is the fourth part in a 5 part series. We’ve looked at Xamarin.Forms and why you should use MvvmCross, how to create your Xamarin.Forms project (Part 1), how to include MvvmCross in your Xamarin.Forms project (Part 2), and how to get your app running on Android (Part 3). In this part, we’ll look at what it takes to get your app up and running on Windows Phone.

Open your solution from the first three parts and carry on.

In the WinPhone project:

  1. Add the following to the constructor in App.xaml.cs:
    // Xamarin.Forms setup
    var setup = new Setup(RootFrame);
    setup.Initialize();
    
  2. Add the following to App.xaml.cs Application_Launching:
    RootFrame.Navigating += RootFrameOnNavigating;
    
  3. Add the following to App.xaml.cs:
    private void RootFrameOnNavigating(object sender, NavigatingCancelEventArgs args)
    {
    	args.Cancel = true;
    	RootFrame.Navigating -= RootFrameOnNavigating;
    	RootFrame.Dispatcher.BeginInvoke(() =>
    	{
    		Mvx.Resolve<IMvxAppStart>().Start();
    	});
    } 
    
  1. Delete the ToDo-MvvmCross folder
  2. Add the Helpers/MvxFormsWindowsPhonePagePresenter class
    1. This class came from a comment in the Xamarin forums: http://forums.xamarin.com/discussion/comment/76871/#Comment_76871
      public sealed class MvxFormsWindowsPhonePagePresenter : IMvxPhoneViewPresenter
      {
      	public static NavigationPage NavigationPage;
      
      	private PhoneApplicationFrame _rootFrame;
      
      	public MvxFormsWindowsPhonePagePresenter(
      		PhoneApplicationFrame rootFrame)
      	{
      		_rootFrame = rootFrame;
      	}
      
      	public async void Show(MvxViewModelRequest request)
      	{
      		if (await TryShowPage(request))
      			return;
      
      		Mvx.Error("Skipping request for {0}",
      			request.ViewModelType.Name);
      	}
      
      	private async Task<bool> TryShowPage(MvxViewModelRequest request)
      	{
      		var page = MvxPresenterHelpers.CreatePage<Page>(request);
      		if (page == null)
      			return false;
      
      		var viewModel = MvxPresenterHelpers.LoadViewModel(request);
      
      		if (NavigationPage == null)
      		{
      			Xamarin.Forms.Forms.Init();
      			NavigationPage = new NavigationPage(page);
      			_rootFrame.Navigate(new Uri("/MainPage.xaml",
      				UriKind.Relative));
      		}
      		else
      		{
      			await NavigationPage.PushAsync(page);
      		}
      
      		page.BindingContext = viewModel;
      		return true;
      	}
      
      	public async void ChangePresentation(MvxPresentationHint hint)
      	{
      		if (hint is MvxClosePresentationHint)
      		{
      			await NavigationPage.PopAsync();
      		}
      	}
      }
      
  1. Update Setup to override CreateViewPresenter method and return the newly created presenter:
    protected override IMvxPhoneViewPresenter CreateViewPresenter(PhoneApplicationFrame rootFrame)
    {
    	var presenter = new MvxFormsWindowsPhonePagePresenter(rootFrame);
    	return presenter;
    }
    
  2. In Main.xaml.cs, replace the previously commented out call to LoadApplication with:
    var navigationPage = MvxFormsWindowsPhonePagePresenter.NavigationPage;
    Content = navigationPage.ConvertPageToUIElement(this);
    

That’s it! You should now have your app up and running on both Android and Windows Phone. We’re almost there! In the final part of the series, we’ll get your app up and running on iOS.

You’ve just read part 4 of the “Comprehensive Guide to Creating a Xamarin.Forms App with MvvmCross” blog series. To read part 5, 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 19, 2021 // blog
    Magenic has agreed to join Cognizant Softvision
  • JANUARY 8, 2021 // blog
    Security In Five Bi-Weekly Roundup – 1/8/21
  • JANUARY 6, 2021 // blog
    The Value of Code Reviews
Featured Content:
  • NOVEMBER 20, 2020 // White Paper
    7 Key Factors for Cloud Success

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