Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Terminate Xamarin Forms App | Quitting Xamarin Forms App

$
0
0

Is there any other better way to terminate Xamarin Forms App ? Than this ?

In Main Project PCL

MainPage.Xaml.cs(The Page Where i want to Terminate)

    protected override bool OnBackButtonPressed()
            {
                Device.BeginInvokeOnMainThread(async () =>
                {
                    var result = await this.DisplayAlert("Alert!", "want to exit?", "Yes", "No");
                    if (result)
                    {
                      var closer = DependencyService.Get<ICloseApplication>();
                      closer?.closeApplication();
                    }
                });
                return true;
            }

Interface

         public interface ICloseApplication
            {
                void closeApplication();
            }

Android

  [assembly: Xamarin.Forms.Dependency(typeof(CloseApplication))]
namespace XForms.Droid
{
    public class CloseApplication : ICloseApplication
    {
        public void closeApplication()
        {
            Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
        }
    }
}

IOS

 [assembly: Xamarin.Forms.Dependency(typeof(CloseApplication))]
namespace XForms.Droid
{
         public class CloseApplication : ICloseApplication
                {
                    public void closeApplication()
                    {
                        Thread.CurrentThread.Abort();
                    }
                }
        }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>