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.

时间: 2024-08-24 14:13:33

Xamarin Forms ch2 - Xlabs的相关文章

Xamarin Forms ch1 - Project Architecture

Before we start any project, the structure of project contents including folder name, third party package referencing need to be settled properly. In this very first section of Xamarin Tutorial, the detailed planning of Xamarin Form project is illurs

使用Xamarin Forms+XLabs 完成 Camara access

1. 安装nugetXamarin.FormsXLabs.Forms 2. MainActivity.cs  (Android) public class MainActivity : XFormsApplicationDroid { /// <summary> /// Called when [create]. /// </summary> /// <param name="bundle">The bundle.</param> pro

菜鸟的Xamarin.Forms前行之路——原生Toast的简单实现方法

项目中信息提示框,貌似只有个DisplayAlert,信息提示太过于单一,且在有些场合Toast更加实用,以下是一个简单的原生Toast的实现方法 项目地址:https://github.com/weiweu/TestProject/tree/dev/Toast 共享项目 定义一个接口IToast,包括Short和Long两个方法: public interface IToast { void LongAlert(string message); void ShortAlert(string m

张高兴的 Xamarin.Forms 开发笔记:为 Android 与 iOS 引入 UWP 风格的汉堡菜单 ( MasterDetailPage )

所谓 UWP 样式的汉堡菜单,我曾在"张高兴的 UWP 开发笔记:汉堡菜单进阶"里说过,也就是使用 Segoe MDL2 Assets 字体作为左侧 Icon,并且左侧使用填充颜色的矩形用来表示 ListView 的选中.如下图 但怎样通过 Xamarin.Forms ,将这一样式的汉堡菜单带入到 Android 与 iOS 中呢? 一.大纲-细节模式简介 讲代码前首先来说说这种导航模式,官方称"大纲-细节模式"(MasterDetail).左侧的汉堡菜单称为&qu

菜鸟的Xamarin.Forms前行之路——绪言

作者入门时间不是很久,差不多一年,期间自学的东西比较杂乱,到目前为止,编程方面的知识比较薄弱.之所以做这个系列,也只是因为做了两个月的Xamarin.Forms方面的东西,由于资料和自身实力的原因,过程走的比较艰难,但所幸的是也解决了部分的问题,积累了一些经验.期望通过这个系列,和大家分享经验,查漏纠错. 作为一个菜鸟,在解决问题的时候,往往比较直接,就是仅仅为了解决问题,期间可能根本没有考虑性能等方面的问题.所以在这个系列中,问题肯定是作者亲身实践能够解决的,但是在性能资源等方面作者没有做过考

Xamarin.Forms开发APP

Xamarin.Forms+Prism(1)-- 开发准备 准备: 1.VS2017(推荐)或VS2015: 2.JDK 1.8以上: 3.Xamarin.Forms 最新版: 4.Prism 扩展,打开VS的扩展和更新,在联机中,搜索Prism,安装第一个扩展Prism Template Pack: 5.Android SDK,这个下载已经非常快了,国内已经支持Android环境下载. 6.都准备好后,请确保创建一个新的Xamarin.Forms程序后,能正常调试运行,不能调试运行的,请百度或

Xamarin.Forms之MessagingCenter

MessagingCenter是Xamarin.Forms中很有用但是很难用的东西,使用起来很方便,但是要注意的地方很多 1.Sender能为空 MessagingCenter.Send<CacheUserInfo>(UserInfo.Instance.CurrentLoginUser, "UpdateMenuIconList"); CurrentLoginUser为空的时候,会直接造成界面卡死(不执行下面的代码,但是也不报错,坑爹的地方)

Xamarin.Forms之Effects的使用

在 Xamarin.Forms 2.1.0-pre1 ,Xamarin.Forms新增了新的Effects API. Effects是一系列方法,为了给View的渲染器添加运行时改变. 然而,我想强调的是, Effects天生被设计为高可复用的. 如果一个Effect能够解决一个难题, 它可能能够在你的整个APP中使用.如果你写了一个Effect来解决你的一个难题, 你能够分享它给其他遇到同样问题的人. 这篇文章尝试展示一种方式,能够帮助我们使分享Effects这件事变得很简单. public

菜鸟的Xamarin.Forms前行之路——按钮的按下抬起事件的监控(可扩展至其他事件)

提问:监控按钮的点击事件,可以通过按钮的Click事件,或者Command绑定,那么如何监控按钮的按下与抬起,或者移动,长按,双击等事件? 解决方法:各个平台自定义渲染依赖注入. 共享项目PCL: 1先定义一个继承Button的实体类NewButton.cs public class NewButton : Button { public event EventHandler Pressed; public event EventHandler Released; public virtual