Xamarin.Forms 多页面显示快速入门

本快速入门演示了如何通过添加第二个屏幕来扩展 Phoneword 应用程序,以跟踪应用程序的呼叫历史记录。 最终的应用程序如下所示:

扩展 Phoneword 应用程序,如下所示:

  1. 在“开始”屏幕中,启动 Visual Studio。 这会打开起始页:

  2. 在 Visual Studio 中,单击“打开项目…”,然后在“打开项目”对话框中选择 Phoneword 项目的解决方案文件。 或者,从“最近”项目列表打开项目:

  3. 在“解决方案资源管理器”中,右键单击“Phoneword”项目,然后选择“添加”>“新建项...”:

  4. 在“添加新项”对话框中,选择“Visual C#”>“跨平台”>“Forms Xaml 页面”,将新文件命名为“CallHistoryPage”,然后单击“添加”按钮。 这会将一个名为 CallHistoryPage 的页面添加到项目:

  5. 在 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">
        <ContentPage.Padding>
            <OnPlatform x:TypeArguments="Thickness"
                        iOS="20, 40, 20, 20"
                        Android="20, 20, 20, 20"
                        WinPhone="20, 20, 20, 20" />
        </ContentPage.Padding>
        <ContentPage.Content>
            <StackLayout VerticalOptions="FillAndExpand"
                         HorizontalOptions="FillAndExpand"
                         Orientation="Vertical"
                         Spacing="15">
                <ListView ItemsSource="{x:Static local:App.PhoneNumbers}" />
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>

    通过按 Ctrl+S,保存对 CallHistoryPage.xaml 所做的更改,然后关闭文件。

  6. 在“解决方案资源管理器”中,双击 App.xaml.cs 将其打开:

  7. 在 App.xaml.cs 中,导入 System.Collections.Generic 命名空间,添加 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());
            }
            ...
        }
    }

    通过按 Ctrl+S,保存对 App.xaml.cs 所做的更改,然后关闭文件。

  8. 在“解决方案资源管理器”中,双击 MainPage.xaml 将其打开:

  9. 在 MainPage.xaml 中,在 StackLayout 控件末尾处添加 Button 控件。 此按钮用于导航到呼叫历史记录页:

    <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>

    按 Ctrl+S,保存对 MainPage.xaml 所做的更改,然后关闭文件。

  10. 在“解决方案资源管理器”中,双击 MainPage.xaml.cs 将其打开:

  11. 在 MainPage.xaml.cs 中,添加 OnCallHistory 事件处理程序方法,并修改 OnCall 事件处理程序方法,从而将已翻译的电话号码添加到 App.PhoneNumbers 集合并启用 callHistoryButton(前提是 dialer 变量不为 null):

    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 ());
            }
        }
    }

    按 Ctrl+S,保存对 MainPage.xaml.cs 所做的更改,然后关闭文件。

  12. 在 Visual Studio 中,选择“生成”>“生成解决方案”菜单项,或按 Ctrl+Shift+B。 将生成应用程序,Visual Studio 状态栏中将显示一条成功消息:

    如果发生错误,请重复前面的步骤并更正任何错误,直到成功生成应用程序。

  13. 在“解决方案资源管理器”中,右键单击“Phoneword.UWP”项目,然后选择“设为启用项目”:

  14. 在 Visual Studio 工具栏中,按“开始”按钮(类似“播放”按钮的三角形按钮),启动应用程序:

     

  15. 在“解决方案资源管理器”中,右键单击“Phoneword.Droid”项目,然后选择“设为启用项目”。

  16. 在 Visual Studio 工具栏中,按“开始”按钮(类似“播放”按钮的三角形按钮),启动 Android 模拟器内的应用程序。
  17. 如果拥有 iOS 设备并符合 Xamarin.Forms 开发的 Mac 系统要求,请使用类似技术将应用部署到 iOS 设备。

    注意:所有模拟器都不支持电话呼叫。

祝贺你!你已完成多屏幕 Xamarin.Forms 应用程序。 本指南的下一个主题将回顾此演练中采用的步骤,以便了解使用 Xamarin.Forms 进行页面导航和数据绑定的知识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值