2017-12-22 18 views

答えて

2

私は小さな例で以下をテストしました。 (UWP上でテストして動作していますが、OnSleep()がアプリケーションを終了するとが呼び出されます)。 OnSleep() App.xaml.csでオーバーライドできるメソッドは、探しているメソッドです。

Xamarin Application LifeCycleには、必要な方法がいくつか用意されています。

OnStart - Called when the application starts. 

OnSleep - Called each time the application goes to the background. 

OnResume - Called when the application is resumed, after being sent to the background. 

アプリケーション終了ための方法がないことに留意されたいです。通常 の状況(クラッシュではない)では、 コードへの追加の通知なしで、アプリケーションの終了がOnSleep状態の から発生します。

例:

using System; 
using System.Diagnostics; 
using Xamarin.Forms; 

namespace App1 
{ 
    public partial class App : Application 
    { 
     public App() 
     { 
      InitializeComponent(); 

      if (Device.RuntimePlatform == Device.iOS) 
       MainPage = new MainPage(); 
      else 
       MainPage = new NavigationPage(new MainPage()); 
     } 

     protected override void OnStart() { 
      Debug.WriteLine("OnStart"); 
     } 
     protected override void OnSleep() { 
      Debug.WriteLine("OnSleep"); 
     } 
     protected override void OnResume() { 
      Debug.WriteLine("OnResume"); 
     } 
    } 
} 

更新

あなたはネイティブコードで例外を未処理キャッチする必要がありthisによります。それはあなたのサービスをシャットダウンするのに苦労します。

例:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { 
     protected override void OnCreate(Bundle bundle) { 
      TabLayoutResource = Resource.Layout.Tabbar; 
      ToolbarResource = Resource.Layout.Toolbar; 
      AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; 
      base.OnCreate(bundle); 

      global::Xamarin.Forms.Forms.Init(this, bundle); 

      LoadApplication(new App()); 
     } 

     private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) { 
      //crashed by exception 
     } 
    } 

はまた、未処理の例外で読む:平方VS2017 `ストップdebugging`をクリックすると、アプリがクラッシュしたり、私はそれがしかし:)作品を試みたhere

+0

、どのように対処します。あなたの答えをありがとう:) – Emixam23

+0

未処理の例外のイベントを追加しました。 –

+0

別のイベントハンドラを追加 - 発生する可能性のあるほぼすべての終了/クラッシュをキャッチするのに十分なはずです –

関連する問題