In this post I’m going to extend the TipCalc to include Xamarin.Forms targets, similar to what we did in the post MVX=0F: A first MvvmCross Application (MVX+1 days of MvvmCross)
Adding Xamarin.Forms
Note: The following instructions can be applied to any project by simply replacing TipCalc with the name of your project
- Add a New Project based on the Mobile App (Xamarin.Forms) template
- In the New Cross Platform App dialog, select Blank App, check the Platforms you want, select .NET Standard and click OK
- Upgrade the Xamarin.Forms NuGet to latest for all four Forms projects
- Add MvvmCross NuGet Package to all Forms projects (Forms, Forms.iOS, Forms.Android and Forms.UWP)
- Add MvvmCross.Forms NuGet Package to all Forms projects (Forms, Forms.iOS, Forms.Android and Forms.UWP)
Update the TipCalc.Forms project
- Remove all code in App class except for constructor with a call to InitializeComponent
- Create Views folder
- Move MainPage into Views folder and rename to FirstView
- Adjust FirstView.xaml and FirstView.xaml.cs to change class name to FirstView and to make it inherit from MvxContentPage
Update the FirstDemo.Forms.Uwp project
- Update Microsoft.NETCore.UniversalWindowsPlatform
- Add reference to TipCalc.Core
- Change MainPage to inherit from MvxFormsWindowsPage
- Remove all code other than the InitializeComponent method call in the constructor of MainPage
- Add ProxyMvxApplication
public abstract class ProxyMvxApplication : MvxWindowsApplication<MvxFormsWindowsSetup<Core.App, TipCalc.Forms.App>, Core.App, TipCalc.Forms.App, MainPage> - Change App.xaml and App.xaml.cs to inherit from ProxyMvxApplication
- Remove all code other than the constructor, with a single call to InitializeComponent, in App.xaml.cs
Update the FirstDemo.Forms.Android project
Note (1): If you run into the following error, you may need to rename your project. In this case we renamed it to Forms.Droid (as well as the folder the project resides in)
1>C:Program Files (x86)Microsoft Visual StudioPreviewEnterpriseMSBuildXamarinAndroidXamarin.Android.Common.targets(2088,3): error MSB4018: System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.Note (2): If you’re using the preview build of Visual Studio, you may run into an error: “error XA4210: You need to add a reference to Mono.Android.Export.dll when you use ExportAttribute or ExportFieldAttribute.” If you do, you just need to Add Reference to Mono.Android.Export (search in the Add Reference dialog).
- Change Forms.Android project to target latest Android SDK
- Upgrade Xamarin.Android.Support.* to latest for the Forms.Android project
- Add reference to TipCalc.Core
- Change MainActivity inheritance, remove code except for a constructor:
public class MainActivity : MvxFormsAppCompatActivity<MvxFormsAndroidSetup<Core.App, App>, Core.App, App>
{
}
Update the TipCalc.Forms.iOS project
- Add reference to TipCalc.Core
- Changed inheritance of AppDelegate
public partial class AppDelegate : MvxFormsApplicationDelegate<MvxFormsIosSetup<Core.App, TipCalc.Forms.App>, Core.App, TipCalc.Forms.App>
Adding TipCalc Xamarin.Forms Layout
Update the TipCalc.Forms project by updating the FirstView.xaml with the following XAML.
<?xml version=”1.0″ encoding=”utf-8″ ?>
<views:MvxContentPage
http://xamarin.com/schemas/2014/forms"”>http://xamarin.com/schemas/2014/forms”
“>“
x_Class=”TipCalc.Forms.Views.FirstView”>
<StackLayout>
<Label Text=”SubTotal”/>
<Editor Text=”{Binding SubTotal, Mode=TwoWay}”/>
<Label Text=”How generous?”/>
<Slider Value=”{Binding Generosity, Mode=TwoWay}”
Minimum=”0″
Maximum=”100″/>
<Label Text=”Tip:”/>
<Label Text=”{Binding Tip}”/>
<Label Text=”SubTotal:”/>
<Label Text=”{Binding Total}”/>
</StackLayout>
</views:MvxContentPage>
That’s it – there’s nothing more to do in order to add Xamarin.Forms targets (iOS, Android and UWP) to the TipCalc