如何终止 Xamarin 应用程序? [英] How to terminate a Xamarin application?

查看:38
本文介绍了如何终止 Xamarin 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从任何活动中终止 Xamarin 应用程序?

How to terminate a Xamarin application from any of the activities?

我已经尝试了 System.Environment.Exit(0)System.Environment.Exit(1) 以及 Finish()> 并杀死所有活动.

I have tried both System.Environment.Exit(0) and System.Environment.Exit(1) as well as Finish() and killing all the activities.

它仍然打开一个带有默认活动名称和黑屏的空白页面.

It still opens one blank page with default activity name and a black screen.

有没有具体的解决方案?

Is there any specific solution for this?

推荐答案

如果您使用 Xamarin.Forms,请创建依赖项服务.

If you are using Xamarin.Forms create a Dependency Service.

界面

public interface ICloseApplication
{
    void closeApplication();
}

Android :使用 FinishAffinity() 不会重新启动您的活动.它只会关闭应用程序.

Android : Using FinishAffinity() won't restart your activity. It will simply close the application.

public class CloseApplication : ICloseApplication
{
    public void closeApplication()
    {
        var activity = (Activity)Forms.Context;
        activity.FinishAffinity();
    }
}

IOS:正如上面已经建议的那样.

IOS : As already suggested above.

public class CloseApplication : ICloseApplication
{
    public void closeApplication()
    {
        Thread.CurrentThread.Abort();
    }
}

UWP

public class CloseApplication : ICloseApplication
{
    public void closeApplication()
    {
        Application.Current.Exit();
    }
}

在 Xamarin 表单中的使用

var closer = DependencyService.Get<ICloseApplication>();
    closer?.closeApplication();

这篇关于如何终止 Xamarin 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆