c#開發移動APP-Xamarin入門擴展
阿新 • • 發佈:2018-10-22
函數 new .cn != 聲明 sem odin code 應用程序 原文:c#開發移動APP-Xamarin入門擴展
這節主要演示了如何通過添加第二個屏幕來跟蹤應用程序的call歷史來擴展Phoneword應用程序。最終如下:
按如下步驟擴展Phoneword
在Phoneword項目右鍵新建Content Page,命名為CallHistoryPage
修改後CallHistoryPage.xaml如下:
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-namespace:Phoneword;assembly=Phoneword" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Phoneword.CallHistoryPage" Title="Call History"> <ContentPage.Padding> <OnPlatform x:TypeArguments="Thickness"><On Platform="iOS" Value="20, 40, 20, 20" /> <On Platform="Android, UWP" Value="20" /> </OnPlatform> </ContentPage.Padding> <StackLayout> <ListView ItemsSource="{x:Static local:App.PhoneNumbers}" /> </StackLayout></ContentPage>
雙擊Phoneword項目的App.xaml.cs文件
添加PhoneNumbers屬性的聲明,在App構造函數中初始化該屬性,並將MainPage屬性初始化為NavigationPage。PhoneNumbers被用於存儲應用程序打的每個電話號碼
using System.Collections.Generic; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation(XamlCompilationOptions.Compile)] namespace Phoneword { public partial class App : Application { public static IList<string> PhoneNumbers { get; set; } public App() { InitializeComponent(); PhoneNumbers = new List<string>(); MainPage = new NavigationPage(new MainPage()); } ... } }
雙擊Phoneword項目的MainPage.xaml文件
在StackLayout控件的末尾添加一個按鈕控件,該按鈕被用於導航到撥打歷史頁面
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Orientation="Vertical" Spacing="15"> ... <Button x:Name="callButton" Text="Call" IsEnabled="false" Clicked="OnCall" /> <Button x:Name="callHistoryButton" Text="Call History" IsEnabled="false" Clicked="OnCallHistory" /> </StackLayout>
打開隱藏文件MainPage.xaml.cs,添加OnCallHistory事件處理程序方法,並修改OnCall事件處理程序方法,將轉換後的電話號碼添加到App.PhoneNumbers集合中,並且如果dialer變量不為null時使能callHistoryButton
using System; using Xamarin.Forms; namespace Phoneword { public partial class MainPage : ContentPage { ... async void OnCall(object sender, EventArgs e) { ... if (dialer != null) { App.PhoneNumbers.Add (translatedNumber); callHistoryButton.IsEnabled = true; dialer.Dial (translatedNumber); } ... } async void OnCallHistory(object sender, EventArgs e) { await Navigation.PushAsync (new CallHistoryPage ()); } } }
下節剖析該擴展
c#開發移動APP-Xamarin入門擴展