项目介绍
以Notes项目为例,The Notes application consists of one solution containing four projects, as shown in the following screenshot:
The projects are:
- Notes – This project is the .NET Standard library project that holds all of the shared code and shared UI.【包含所有共享代码和共享 UI 的 .NET Standard 库项目】
- Notes.Android – This project holds Android-specific code and is the entry point for the Android application.
- Notes.iOS – This project holds iOS-specific code and is the entry point for the iOS application.
- Notes.UWP – This project holds Universal Windows Platform (UWP) specific code and is the entry point for the UWP application.
程序剖析
1、.NET Standard 库项目
包含 NuGet 和 SDK :
- NuGet–已添加到项目的 Xamarin 和 sqlite-pcl NuGet 包。
- SDK –
NETStandard.Library元包。
2、项目还包括多个文件:
- Data\NoteDatabase.cs –此类包含用于创建数据库、从中读取数据、向其中写入数据和从中删除数据的代码。
- Models\Note.cs –此类定义一个
Note模型, 其实例存储有关应用程序中每个注释的数据。 - App.xaml -
App类的 XAML 标记,该类定义应用程序的资源字典。 - App.xaml.cs -
App类的代码隐藏,该类负责实例化应用程序在每个平台上将显示的首页,并处理应用程序生命周期事件。 - AssemblyInfo.cs –此文件包含项目的应用程序属性, 该属性应用于程序集级别。
- NotesPage -
NotesPage类的 xaml 标记, 用于定义在应用程序启动时显示的页的 UI。 - NotesPage.xaml.cs -
NotesPage类的代码隐藏, 该类包含用户与页面交互时执行的业务逻辑。 - NoteEntryPage -
NoteEntryPage类的 xaml 标记, 它定义用户输入备注时显示的页面的 UI。 - NoteEntryPage.xaml.cs -
NoteEntryPage类的代码隐藏, 该类包含用户与页面交互时执行的业务逻辑。
体系结构和应用程序基础知识
共享代码通常位于 .NET Standard 库中,平台特定应用程序将使用此共享代码。
- App.xaml
new NotesPage());
- AssemblyInfo.cs
XamlCompilation特性用于控制在生成时还是运行时编译 XAML , 以便将 xaml 直接编译为中间语言。
启动应用程序
- IOS
若要在 iOS 中启动最初的Xamarin. Forms 页面, Notes.ios 项目定义了从FormsApplicationDelegate类继承的AppDelegate类。
namespace Notes.iOS { [Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); return base.FinishedLaunching(app, options); } } }