c#開發移動APP-Xamarin入門剖析
剖析應用程式
Phoneword這個專案是.net標準庫專案,它包含所有的共享程式碼和共享UI。
Phoneword.Android這個專案包含Android特定的程式碼,是Android應用程式的入口點。
Phoneword.iOS -這個專案包含iOS特定的程式碼,是iOS應用程式的入口點。
Phoneword.UWP -這個專案包含通用Windows平臺(UWP)特定的程式碼,是UWP應用程式的入口點
下面的截圖顯示了Visual Studio中Phoneword .NET標準庫專案的內容:
App. XAML——App類的
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)]View Code4 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>