Wednesday 25 October 2017

Xamarin forms Back Button Pressed Gives Alert Box To Exit [Tutorial 24]



Step1:
App.Xaml.cs
InitializeComponent();
MainPage = new App32.MainPage();




Step2:

MainPage.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"
             xmlns:local="clr-namespace:App32"
             x:Class="App32.MainPage"
              BackgroundImage="school.jpg">
    <ContentPage.Content>

        <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
            <Entry Placeholder="School Home-Ground"/>
            <Button Text="Click To Enter " BackgroundColor="White" TextColor="Black" Clicked="Button_OnClicked" Image="myicon.png"></Button>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>



Step3: App.Xaml.cs
        private void Button_OnClicked(object sender, EventArgs e)
        {
            DisplayAlert("Important-Notification", "Do you want Play Baseball ?", "Yes", "No");
        }

        protected override bool OnBackButtonPressed()
        {
            //Device.BeginInvokeOnMainThread(async () => {
            //    //var result = await this.DisplayAlert("Alert!", "Do you really want to exit?", "Yes", "No");
            //    //if (result) await this.Navigation.PopAsync(); // or anything else
            //});

            return false;
        }
    }
}

1 comment:

  1. In the world of www, there are countless blogs. But believe me, this blog has all the perfection that makes it unique in all. I will be back again and again. www.exit-5.net

    ReplyDelete