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 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:
  • FEBRUARY 23, 2021 // blog
    Stronger Product Owner = Better Software, Faster
  • FEBRUARY 19, 2021 // blog
    Security In Five Bi-Weekly Roundup – 2/19/21
  • FEBRUARY 18, 2021 // blog
    Doesn’t Everybody Know This?
Featured Content:
  • JANUARY 25, 2021 // White Paper
    The Top 7 Technology Trends of 2021

Related Posts

Blog
What iOS 11 64-bit Support Means for Your Mobile App
Learn More
Blog
Microsoft Buys Xamarin - Analysis
Learn More
Blog
Magenic Named a Xamarin Elite Consulting Partner
Learn More
Blog
A Comprehensive Guide to Creating a Xamarin.Forms App with MvvmCross (Part 5 of 5)
Learn More

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