Xamarin Forms ch2 - Xlabs

Xamarin.iOS and Xamarin.Droid proved that C# code can be used to develop mobile apps, and majority of business logic written in c# can be 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 iOS 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 that 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 to use functions, such as custom controls (extendedEntry, GridView, WebImage), services (Camera, Device, Display) and helpers including IOC and MVVM. In this section, I will 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, SampleArchitecture.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 public App ()
 2 {
 3     RegisterViews ();
 4 
 5     MainPage = new NavigationPage ((Page)ViewFactory.CreatePage<HomeViewModel, Home>());
 6 }
 7 
 8 private void RegisterViews()
 9 {
10     ViewFactory.Register<Home, HomeViewModel> ();
11 }

 

3.3 Setup up Android Project

Update MainActivity class

(1) Change base class to XLabs.Froms.XFormsApplicationDroid

(2) Set resolver with Autofac

 1     public class MainActivity : XLabs.Forms.XFormsApplicationDroid
 2     {
 3         protected override void OnCreate (Bundle bundle)
 4         {
 5             Xamarin.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) {
10                 SetIoc ();
11             }
12                 
13             LoadApplication (new App ());
14         }
15 
16         private void SetIoc()
17         {
18             var container = new Autofac.ContainerBuilder ();
19             Resolver.SetResolver (new AutofacContainer (container.Build ()));
20         }
21     }

 

3.4 Set up iOS project

(1) Change AppDelegate base class to Xlabs.Forms.XFormsApplicationDelegate

(2) set resolver with Autofac

    [Register ("AppDelegate")]
    public partial class AppDelegate : XLabs.Forms.XFormsApplicationDelegate
    {
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init ();

            if (!Resolver.IsSet) {
                SetIoc ();
            }
            
            LoadApplication (new App ());

            return base.FinishedLaunching (app, options);
        }

        private void SetIoc()
        {
            var container = new Autofac.ContainerBuilder ();
            Resolver.SetResolver (new AutofacResolver (container.Build()));
        }
    }

 

Now the project should be compiled successfully (cmd + k). And if you run the project, you will be able to see text "hello xamarin" in the center of the mobile screen.  

转载于:https://www.cnblogs.com/timBo/p/5210007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值