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:
- 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; } }
- Delete the Views folder
- Delete the ToDo-MvvmCross folder
- Add a Helpers folder
- Create a Helpers/MvxPagePresenter class
- 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() { } }
- 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.