Xamarin.ios and Xamarin.droid proved that C # code can is used to develop mobile apps, and majority of business logic Writt En in C # can is shared on both mobile platforms development. However, the development of User Interface is still heavily depending on platform ' s specific code, such as storyboard on I OS and XML for Android. Xamarin Form is an approach-to-build native UIs for IOS, Android and Windows from a single, shared C # code base.
Xamarin Forms Labs is a open source project this aims to provide a powerful and cross platform set of controls and helpers Tailored to work with Xamain Forms. The package shipped with many useful and ready for use functions, such as custom controls (Extendedentry, GridView, Webimag e), Services (Camera, Device, Display) and helpers including IOC and MVVM. In this section, I'll look into the use of these helpful tools and see how it helps with the development.
1. Add Package
The first step is to add Xlabs package to the project. You can find the instruction at my previous tutorial. And do remember to add those package to all three of you projects (Samplearchitecture, Samplearchitecture.ios, Samplearchi Tecture. Droid).
2. MVVM and AUTOFAC support in Xlabs
2.1 Create Home View and ViewModel
Homeviewmodel should inherite from Xlabs.Forms.Mvvm.ViewModel.
3.2 Register View
Xlabs provides us with viewfactory to combine the View with corresponding ViewModel.
At App.cs
1 PublicApp ()2 {3 registerviews ();4 5MainPage =NewNavigationpage (Page) Viewfactory.createpage());6 }7 8 Private voidregisterviews ()9 {TenViewfactory.register (); One}
3.3 Setup up Android Project
Update Mainactivity Class
(1) Change base class to XLabs.Froms.XFormsApplicationDroid
(2) Set resolver with AUTOFAC
1 Public classMainActivity:XLabs.Forms.XFormsApplicationDroid2 {3 protected Override voidOnCreate (Bundle bundle)4 {5Xamarin.Insights.Initialize (Global:: SampleArchitecture.Droid.XamarinInsights.ApiKey, This);6 Base. OnCreate (bundle);7 Global:: Xamarin.Forms.Forms.Init ( This, bundle);8 9 if(!resolver.isset) {Ten setioc (); One } A -Loadapplication (NewApp ()); - } the - Private voidSETIOC () - { - varcontainer =NewAutofac.containerbuilder (); +Resolver.setresolver (NewAutofaccontainer (container. Build ())); - } +}
3.4 Set up IOS Project
(1) Change appdelegate base class to Xlabs.Forms.XFormsApplicationDelegate
(2) Set resolver with AUTOFAC
[Register ("appdelegate")] Public Partial classAppDelegate:XLabs.Forms.XFormsApplicationDelegate { Public Override BOOLfinishedlaunching (uiapplication app, nsdictionary options) {Global:: Xamarin.Forms.Forms.Init (); if(!resolver.isset) {setioc (); } loadapplication (NewApp ()); return Base. Finishedlaunching (app, options); } Private voidSetioc () {varcontainer =NewAutofac.containerbuilder (); Resolver.setresolver (NewAutofacresolver (container. Build ())); } }
Now the project should is compiled successfully (cmd + k). And if you run the project, you'll be able to see text "Hello Xamarin" in the center of the mobile screen.
Xamarin Forms Ch2-xlabs