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 illurstrated and explained.

1. Project Architecture

A typical Xamarin Form project consists of many sections, such as a PCL project that contains business logic, UI (either in code or Xaml), Data access, web service connection etc, an Android project  that has android platform specific settings, configurations and resources, an iOS project. In addition, Unit Testing and UI automated testing projects are included as well to improve quality of the code.

2. Folder Structure

Folder, in general, is a useful tool to group related information, and present it with meaningful name. In software project, with well defined folder, the project is more concise and possibly can improve the maintainability. As majority code is sitting in the PCL project, we will pay more attention in the SampleArchitecture project.

Before I explain the detailed folders, I want to make sure everyone is understanding the pattern of MVVM.  The pattern is actually derived from MVC pattern, which enforces the separation of user inferface design to make the code is more testable and clear.

2.1 Model

Model refers the domain design. In object oriented approach, it represents objects in the real world, such as Teacher, Student. And all of the objects will be placed within Model folder, under SampleArchitecture.Model namespace.

2.2 View

View is user interface of the project. In xamarin form, there are two ways of writing UI, either completely in Code or Write Xaml. In the project, I choose Xaml as the syntax is more concise and is well known if the developer has WPF or silverlight background.  So, all mobile pages will be places within view folder, and it can be further divided into subfolder such as Registration, SchoolManagement etc according to project features.

2.3 ViewModel

ViewModel is the bridge between Model and View. It encapsulates the logic data from Model and presents it in the View based on the needs. Hence, most logic and processing code should all be placed within ViewModel.

2.4 Controls

Controls folder, is where we put customized controls. Xamarin form is still a relative new platform, there are controls need to be customized in order to meet specific project requirements. For example, UnderlineEntry is a control that extends class Entry, providing one single line below the input box.

2.5 Helpers

Helpers folder, is the place for utility class which provides functions can be used across the application, such as AppConstant.cs - constant value for project, Settings.cs - project settings. In general, those class are static.

2.6 Network

Network folder, is where we handle network connection.

2.7 Service

Service folder, contains abstract and concrete subfolders. And this is the place we set up the interface of data request and fetching data through web service, then pass the data to the ViewModels.

3. Package architecture


Here is the list of packages that I referenced in this sample project. And the detailed use of those packages will be explained in the following section.

3.1 Autofac

Autofac is a popular dependency injection tool. Please refer to the documentation is you are not familiar with it.

3.2 Xamarin Forms Labs 

XLabs is an open source project that aims to provide a powerful and cross platform set of services and controls tailored to work with Xamain Forms.

3.3 PropertyChanged.Fody

The add-in injects INotifyPropertyChanged code into properties.

3.4 Microsoft.Net.Http

This package includes HttpClient for sending requests over HTTP, as well as HttpRequestMessage and HttpResponseMessage for processing Http Messages.

So far, I went through the first step of starting a typical Xamarin Form project. In the next secion, I will show you how to configure those packages and compile the project successfully.

时间: 2024-06-11 02:29:15

Xamarin Forms ch1 - Project Architecture的相关文章

[Xamarin] - Xamarin.Forms Project with .Net Standard 2.0

1. Install .NET Core 2.0 SDK .https://www.microsoft.com/net/download/core 2. Install Android 7.1 (API 25) SDK. 3. New .NET Standard Project With Nuget Update:Microsoft.EntityFrameworkCore.2.0.0Microsoft.EntityFrameworkCore.Sqlite.2.0.0Xamarin.Forms.2

Xamarin.Forms学习之Platform-specific API和文件操作

这篇文章的分享原由是由于上篇关于Properties的保存不了,调用SavePropertiesAsync()方法也不行,所以我希望通过操作文件的方式保存我的需要的数据,然后我看了一下电子书中的第二十章和需要相关知识的第九章,这篇文章中的内容则是我学习这两章的一点记录和分享,还是那样,有错请留言指正,谢谢! 不同的平台存在着一些特定的API,通过在电子书中两章的学习,实践一下如何调用这些API和将这些API封装成公共的库,供以后的项目调用.以一个显示平台信息的小实例开始做一个简单的演示,其运行效

Xamarin.Forms学习之Page Navigation(一)

在最初接触Xamarin.Forms的时候,我是跟着Xamarin官方的名为“learning-xamarin-ebook”的pdf文档进行学习的,我在成功运行Hello world程序之后,我开始跟着pdf写上面的monkey实例,然而我却遇到了一个问题,我在列表页点击某一个Item的时候,不能如示例上面所展示的那样跳转到详细页面.也正因为此我在官方文档和电子书中寻找答案,也将自己的学习的东西做个简单的笔记和分享. 在Froms的中的提供了如下方法用于导航: Task PushAsync(Pa

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 dependi

【转】Xamarin Forms 介绍

特此声明,本篇博文转自:http://blog.csdn.net/kinfey/article/details/29621381 什么是 Xamarin Forms ? Xamarin Forms 是一个高效创建跨平台用户界面的库 .通过Xamarin Forms 可以一次编码生成基于主流移动平台(iOS, Android, Windows Phone)的应用界面.和HTML 5 不同, 它是一套原生的界面解决方案,这意味着通过Xamarin Forms 渲染的界面是与底层API 紧密相连, 那

Xamarin.Forms Layout Challenges – Great Places(已全文翻译)

原文地址:https://www.kymphillpotts.com/xamarin-forms-layout-challenges-great-places/ (作者Kym Phillpotts) 项目Github地址:https://github.com/kphillpotts/XamarinFormsLayoutChallenges When your app is all about the images, sometimes you want your images to be her

张高兴的 Xamarin.Forms 开发笔记:Android 快捷方式 Shortcut 应用

一.Shortcut 简介 Shortcut 是 Android 7.1 (API Level 25) 的新特性,类似于苹果的 3D Touch ,但并不是压力感应,只是一种长按菜单.Shortcut 是受启动器限制的,也就是说国内大厂的定制系统大多数是不支持的,那些所谓的可以 pin 在桌面上的应用功能的快捷启动图标本质上就是 Shortcut . 二.Shortcut 在 Xamarin.Forms 中的实现分析 本文讨论的是动态 Shortcut 实现. 实现方式无非两种思路,一种 Nat

vs for mac xamarin.forms 将pcl转换为netstandard后报错

报错内容: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } Xamarin.Forms targets have been imported multiple times. Please check your project file and remove the duplicate import(s). (XF001) 解决方案: 经过一番谷歌后,找到解决办法. 原文: If you converted the

菜鸟的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