Xamarin 创建 Alert 与 Lambdas表达式

AlertCallbacksPage.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="one.AlertCallbacksPage">
    <StackLayout>
        <Button Text="Invoke Alert" FontSize="Large" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnButtonClicked"/>
        <Label x:Name="label" Text="Tap Button to invoke alert" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage>

AlertCallbacksPage.xaml.cs:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace one
{
    public partial class AlertCallbacksPage : ContentPage
    {
        public AlertCallbacksPage()
        {
            InitializeComponent();
        }

        void OnButtonClicked(object sender, EventArgs args) {
            Task<bool> task = DisplayAlert("Simple Alert", "Decide on an option", "yes or ok", "no or cancel");
            task.ContinueWith((Task<bool> taskResult) => {
                Device.BeginInvokeOnMainThread( () => {
                    label.Text = String.Format("Alert {0} button was pressed", taskResult.Result ? "OK" : "Cancel");
                });
            });
            label.Text = "Alert is currently displayed";
        }
    }
}