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 5 of 5)

July 17, 2015 // By Brent Edwards

This is the final part of a 5 part series. We’ve looked at Xamarin.Forms and MvvmCross as well as creating a solution with the two of them together. Then we looked at getting that solution running on Android and Windows Phone. You can read part 1, part 2. part 3, and part 4 here. In this part, we will wrap things up by getting your app running on iOS.

We are picking up where we left off, so open your solution from the previous parts.

In the iOS project:

  1. Update AppDelegate according to AppDelegate.cs.txt provided from Nuget package.
    [Register("AppDelegate")]
    public partial class AppDelegate : MvxApplicationDelegate
    {
    	UIWindow _window;
    
    	public override bool FinishedLaunching(UIApplication app,
    		NSDictionary options)
    	{
    		_window = new UIWindow(UIScreen.MainScreen.Bounds);
    
    		var setup = new Setup(this, _window);
    		setup.Initialize();
    
    		var startup = Mvx.Resolve<IMvxAppStart>();
    		startup.Start();
    
    		_window.MakeKeyAndVisible();
    
    		return true;
    	}
    }
    
  2. Delete the Views folder
  3. Delete the ToDo-MvvmCross folder
  4. Add a Helpers folder
  5. Create a Helpers/MvxPagePresenter class
    1. This is a good starting point: https://github.com/Cheesebaron/Xam.Forms.Mvx/blob/master/Movies/Movies.iOS/MvxFormsTouchViewPresenter.cs
      public sealed class MvxPagePresenter : IMvxTouchViewPresenter
      {
      	private readonly UIWindow _window;
      	private NavigationPage _navigationPage;
      	private readonly IUiContext context;
      
      	public MvxPagePresenter(UIWindow window, IUiContext context)
      	{
      		_window = window;
      		this.context = context;
      	}
      
      	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)
      		{
      			_navigationPage = new NavigationPage(page);
      			_window.RootViewController =
      				_navigationPage.CreateViewController();
      			this.context.CurrentContext = _window.RootViewController;
      		}
      		else
      		{
      			await _navigationPage.PushAsync(page);
      		}
      
      		page.BindingContext = viewModel;
      		return true;
      	}
      
      	public async void ChangePresentation(MvxPresentationHint hint)
      	{
      		if (hint is MvxClosePresentationHint)
      		{
      			await _navigationPage.PopAsync();
      		}
      	}
      
      	public bool PresentModalViewController(UIViewController controller,
      		bool animated)
      	{
      		return false;
      	}
      
      	public void NativeModalViewControllerDisappearedOnItsOwn()
      	{
      
      	}
      }
      
  1. Add the following override to Setup:
    protected override IMvxTouchViewPresenter CreatePresenter()
    {
    	return new MvxPagePresenter(Window, Mvx.Resolve<IUiContext>());
    }
    

You should now have a fully functional Xamarin.Forms app that runs on Android, iOS, and Windows Phone! This is only the beginning, but it’s much easier to learn Xamarin.Forms and MvvmCross when you have a functional base to learn from. Now you can focus on learning the technologies instead of the plumbing. After creating a few apps like this, the plumbing will start to make more sense and you can learn where you want to make your own customizations. Happy coding!

Thank you for reading the “Comprehensive Guide to Creating a Xamarin.Forms App with MvvmCross” blog series. If you’d like to contact Magenic to learn more about development using Xamarin, 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 4 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