剖析應用程序
Phoneword這個項目是.net標准庫項目,它包含所有的共享代碼和共享UI。
Phoneword.Android這個項目包含Android特定的代碼,是Android應用程序的入口點。
Phoneword.iOS -這個項目包含iOS特定的代碼,是iOS應用程序的入口點。
Phoneword.UWP -這個項目包含通用Windows平台(UWP)特定的代碼,是UWP應用程序的入口點
下面的截圖顯示了Visual Studio中Phoneword .NET標准庫項目的內容:
App. XAML——App類的XAML標記,它為應用程序定義了一個資源字典。
App.xaml.cs——應用App類的后台代碼,它負責實例化應用程序在每個平台上顯示的第一個頁面,以及處理應用程序生命周期事件。
IDialer.cs—— IDialer接口 指定撥號方法必須由任何實現類提供
MainPage.xaml——MainPage類的xaml標記,它為應用程序啟動時顯示的頁面定義UI。
MainPage.xaml.cs——MainPage類的后台代碼,它包含用戶與頁面交互時執行的業務邏輯。
PhoneTranslator.cs -負責轉換電話號碼,被MainPage.xaml.cs調用。
Xamarin.Form應用程序的架構方式與傳統的跨平台應用程序相同。共享代碼通常放在.net標准庫中,特定於平台的應用程序使用共享代碼。下圖顯示了Phoneword應用程序的這種關系的概述:
為了最大限度地重用啟動代碼,Xamarin.Form應用程序有一個名為App單獨的類,它負責實例化應用程序在每個平台上顯示的第一個頁面,如下面的代碼示例所示:

1 using Xamarin.Forms; 2 using Xamarin.Forms.Xaml; 3 [assembly: XamlCompilation(XamlCompilationOptions.Compile)] 4 namespace Phoneword 5 { 6 public partial class App : Application 7 { 8 public App() 9 { 10 InitializeComponent(); 11 MainPage = new MainPage(); 12 } 13 ... 14 } 15 }
這段代碼將App類的MainPage 屬性設置為MainPage 類的新實例。此外,XamlCompilation屬性會打開XAML編譯器,從而將XAML直接編譯為中間語言。更多信息查看 XAML Compilation.
在每個平台上啟動應用程序
iOS
ios上為了運行首頁,iOS項目包含從FormsApplicationDelegate類繼承而來的AppDelegate類,如下面的代碼示例所示
namespace Phoneword.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); } } }
FinishedLaunching通過調用Init方法重寫Xamarin.Forms框架的初始化,這將導致Xamarin.Forms 在IOS平台的實現在通過調用LoadApplication方法設置根視圖控制器之前加載
Android
在Android上為了運行首頁,Phoneword.Droid項目包含使用MainLauncher屬性創建Activity的代碼,該activity 繼承自FormsAppCompatActivity類,如下面的代碼示例所示
namespace Phoneword.Droid { [Activity(Label = "Phoneword", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { internal static MainActivity Instance { get; private set; } protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(bundle); Instance = this; global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App()); } } }
OnCreate通過調用Init方法重寫初始化Xamarin.Form框架,這將導致安卓特定平台的Xamarin.Forms的實現被加載在Xamarin.Forms 程序加載前。此外,MainActivity類在Instance屬性中存儲對自身的引用。Instance屬性稱為本地上下文,並被PhoneDialer類引用。
UWP
初始化Xamarin.Form框架的Init方法在App類被調用:
Xamarin.Forms.Forms.Init (e); if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { ... }
Xamarin.Forms首頁在MainPage類中被啟動
namespace Phoneword.UWP { public sealed partial class MainPage { public MainPage() { this.InitializeComponent(); this.LoadApplication(new Phoneword.App()); } } }
通過LoadApplication方法Xamarin.Forms 程序被加載
用戶界面
有四個主要的用於創建Xamarin.Forms 應用程序的用戶界面的控件組。
Pages -Xamarin.Forms頁面表示跨平台的移動應用程序屏幕。Phoneword應用程序使用ContentPage類顯示單個屏幕。
Layouts-Xamarin.Forms布局是用來將視圖組合成邏輯結構的容器。Phoneword應用程序使用StackLayout類在水平堆棧中排列控件。
Views-Xamarin.Forms視圖是顯示在用戶界面上的控件,例如標簽、按鈕和文本輸入框。Phoneword應用程序使用標簽、條目和按鈕控件。
Cells-Xamarin.Forms單元格是用於列表中的項的特殊元素,並描述如何繪制列表中的每個項
當Phoneword應用程序在任何平台上運行時,它會顯示一個對應於Xamarin.Forms頁面的屏幕。頁面對應Android中的ViewGroup、iOS中的視圖控制器或通用Windows平台上的頁面。Phoneword應用程序還實例化了一個表示MainPage類的ContentPage對象,其XAML標記顯示在以下代碼示例中:
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Phoneword.MainPage"> ... <StackLayout> <Label Text="Enter a Phoneword:" /> <Entry x:Name="phoneNumberText" Text="1-855-XAMARIN" /> <Button x:Name="translateButon" Text="Translate" Clicked="OnTranslate" /> <Button x:Name="callButton" Text="Call" IsEnabled="false" Clicked="OnCall" /> </StackLayout> </ContentPage>
下面的代碼示例顯示了MainPage類的隱藏代碼中的OnTranslate方法,該方法在Translate按鈕上觸發單擊事件時會執行。
void OnTranslate(object sender, EventArgs e) { translatedNumber = Core.PhonewordTranslator.ToNumber (phoneNumberText.Text); if (!string.IsNullOrWhiteSpace (translatedNumber)) { callButton.IsEnabled = true; callButton.Text = "Call " + translatedNumber; } else { callButton.IsEnabled = false; callButton.Text = "Call"; } }
XAML類的代碼隱藏文件可以訪問XAML中定義的對象,通過使用 x:Name屬性賦給它的名稱
Phoneword中引入的其他概念
1-顯示警告對話框
await this.DisplayAlert ( "Dial a Number", "Would you like to call " + translatedNumber + "?", "Yes", "No");
2-通過DependencyService類訪問本機特性
Phoneword 使用DependencyService類解析特定平台對IDialer接口接口的實現
async void OnCall (object sender, EventArgs e) { ... var dialer = DependencyService.Get<IDialer> (); ... }
3-使用URL撥打電話
Phoneword應用程序使用OpenURL啟動系統電話應用程序。URL由tel:前綴和要調用的電話號碼組成,如下iOS項目代碼示例所示:
return UIApplication.SharedApplication.OpenUrl (new NSUrl ("tel:" + number));
4-調整平台布局
Device類允許開發人員在每個平台上定制應用程序布局和功能,如下面的代碼示例所示,它在不同平台上使用不同padding值來正確顯示每個頁面
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" ... > <ContentPage.Padding> <OnPlatform x:TypeArguments="Thickness"> <On Platform="iOS" Value="20, 40, 20, 20" /> <On Platform="Android, UWP" Value="20" /> </OnPlatform> </ContentPage.Padding> ... </ContentPage>