[Go] Introduction to xamarin forms

Source: Internet
Author: User

I hereby declare that this blog post is transferred from: http://blog.csdn.net/kinfey/article/details/29621381

What is xamarin forms?

Xamarin forms is a library that efficiently creates cross-platform user interfaces. Xamarin forms can be used to generate an application interface based on mainstream mobile platforms (IOS, Android, and Windows Phone) at one time. Unlike HTML 5, it is a set of Native Interface solutions, which means that the interface rendered through xamarin forms is closely linked to the underlying API, so you can combine core motion such as IOS, passkit also has storekit APIs, and can also use Android APIs such as NFC/Google Play service. Of course, Windows Phone tiles is indispensable.

Advantages of xamarin forms

A multi-platform interface is generated through one encoding. If your work involves three platforms, you will get bored with complex interface logic work. xamarin forms is really a good solution.

Use xamarin forms

You can use the C # Hard encoding method to construct your interface, and you can use the XAML method to build the interface.

Interfaces compatible with xamarin forms

Pages)

Layout)

Controls)

Let's take a look at a simple example. As this article is just a preliminary study, I will only record some of the highlights. You can download the code for details. If you want to explore, continue to follow my blog, and there will be more in-depth articles on xamarin forms.

1. Create an xamarin. Forms project

One thing to note here is that the xamarin Forms project has two templates: Share-based projects and PCL-based projects, I select PCL by default (I will talk about the use of the two templates later)

2. after the cnbetademo is created. shared, cnbetademo. IOS, cnbetademo. three Android projects (if you create with Visual Studio, you will have cnbetademo. windows Phone project ). Cnbetademo. Shared is the shared logic layer and xamarin. forms to be processed. The other two are the platforms we need.

3. According to the mvvm architecture, our cnbetademo. Shared has to be mentioned here. Xamarin is a cross-platform native application solution based on the shared logic layer.

Why does mvvm be used as mentioned in my previous article. (The blog decides to put it in csdn. I will post the address after migration ). The previous year's practice was to share logic. Now we can construct the page through xamarin. forms, so cnbetademo. Shared becomes the core code layer of our cross-platform architecture project.

4. Let's take a look at the viewmodel layer. Here I take reading cnbeta RSS as an example. Some Main Code of feedviewmodel:

Mainly load data through async and await

   1:  private async Task ExecuteLoadItemsCommand()
   2:          {
   3:              if (isBusy)
   4:                  return;
   5:   
   6:              IsBusy = true;
   7:   
   8:              try{
   9:   
  10:                  var httpClient = new HttpClient();
  11:   
  12:                  var feed="http://cnbeta.feedsportal.com/c/34306/f/624776/index.rss";
  13:   
  14:   
  15:                  var responseString = await httpClient.GetStringAsync(feed);
  16:   
  17:                  FeedItems.Clear();
  18:   
  19:                  var items= await ParseFeed(responseString);
  20:   
  21:                  foreach(var item in items)
  22:                  {
  23:   
  24:                      //Console.WriteLine(item.Title);
  25:                      FeedItems.Add(item);
  26:                  }
  27:              }
  28:              catch(Exception ex){
  29:   
  30:                  var page = new ContentPage ();
  31:   
32: var result = page. displayalert ("error", "loading failed.", "OK", null );
  33:   
  34:              }
  35:   
  36:              IsBusy = false;
  37:   
  38:          }
 
 
 
   1:  private async Task<List<FeedItem>> ParseFeed(string rss)
   2:          {
   3:              return await Task.Run (() => {
   4:                  var xdoc=XDocument.Parse(rss);
   5:   
   6:                  var id=0;
   7:   
   8:                  return ( from item in xdoc.Descendants("item")
   9:                      select new FeedItem
  10:                      {
  11:                          Title = (string)item.Element("title"),
  12:                          Description=(string)item.Element("description"),
  13:                          PublishDate=(string)item.Element("pubDate"),
  14:                          Id= id++
  15:                      }).ToList();
  16:              });
  17:          }

5. Construct a view through xamarin forms

Here we need to create a simple form listview and a custom cell. Of course, xamarin forms will still be rendered on the platform to generate native form support.

   1:  public class FeedView : ContentPage
   2:      {
   3:          private FeedViewModel ViewModel{
   4:              get {  return BindingContext as FeedViewModel;  }
   5:          }
   6:   
   7:          public FeedView ()
   8:          {
   9:   
  10:              BindingContext = new FeedViewModel ();
  11:   
  12:              var stack = new StackLayout {
  13:                  Orientation= StackOrientation.Vertical,
  14:                  Padding = new Thickness(0,8,0,8)
  15:              };
  16:   
  17:              var listView = new ListView ();
  18:   
  19:   
  20:              listView.ItemsSource = ViewModel.FeedItems;
  21:   
  22:              var cell = new DataTemplate (typeof(ListTextCell));
  23:   
  24:              cell.SetBinding (TextCell.TextProperty, "Title");
  25:   
  26:              cell.SetBinding (TextCell.DetailProperty, "PublishDate");
  27:   
  28:              listView.ItemTemplate = cell;
  29:   
  30:   
  31:              stack.Children.Add (listView);
  32:   
  33:              Content = stack;
  34:          }
  35:   
  36:          protected override void OnAppearing()
  37:          {
  38:              base.OnAppearing ();
  39:   
  40:              if (ViewModel == null || !ViewModel.CanLoadMore || ViewModel.IsBusy || ViewModel.FeedItems.Count > 0)
  41:                  return;
  42:   
  43:              ViewModel.LoadItemsCommand.Execute (null);
  44:          }
  45:      }

6. Run it.

Welcome to download my code and click to download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.