2017-09-13 67 views
0

我在iOS中的 我的聲明中遇到以下問題。當用戶在後臺時返回到應用程序時,我需要激活一個命令。在我的ContentPage我無法找到一個方法來確定返回。在Android中,它工作正常,只有在iOS上,我找不到在內容的任何功能,顯示了我,我回來在contentpage中調用onresumo()方法XAMARIN FORMS

protected override void OnAppearing() 
    { 
     Debug.WriteLine("OnAppearing()"); 
     base.OnAppearing(); 
    } 

    protected override void OnDisappearing() 
    { 
     Debug.WriteLine("OnDisappearing"); 
     base.OnDisappearing(); 
    } 

    protected override void OnBindingContextChanged() 
    { 
     Debug.WriteLine("OnBindingContextChanged"); 
     base.OnBindingContextChanged(); 
    } 

我嘗試了三個選項,但沒有結果

+0

這是你在找什麼或者? https://forums.xamarin.com/discussion/63577/app-onresume-not-called-on-ios – josemigallas

+0

Onresume在我的App.xaml.cs中正確調用,與我的應用程序一起工作,我將不得不稱呼onresume在內容頁面 –

+0

是的,對不起,沒有意識到你正在使用XF。愚蠢的建議也許,但你有沒有嘗試先調用基本方法? :P – josemigallas

回答

2

我通常做這種所以我不必費心訂閱:

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

     MainPage = new MainPage(); 
    } 

    protected override async void OnStart() 
    { 
     if (Application.Current.MainPage is IAppStateAware appStateAwarePage) 
      await appStateAwarePage.OnStartApplicationAsync(); 
     if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm) 
      await appStateAwareVm.OnStartApplicationAsync(); 
     // Handle when your app starts 
    } 

    protected override async void OnSleep() 
    { 
     if (Application.Current.MainPage is IAppStateAware appStateAwarePage) 
      await appStateAwarePage.OnSleepApplicationAsync(); 
     if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm) 
      await appStateAwareVm.OnSleepApplicationAsync(); 
     // Handle when your app sleeps 
    } 

    protected override async void OnResume() 
    { 
     if (Application.Current.MainPage is IAppStateAware appStateAwarePage) 
      await appStateAwarePage.OnResumeApplicationAsync(); 
     if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm) 
      await appStateAwareVm.OnResumeApplicationAsync(); 
     // Handle when your app resumes 
    } 
} 

public class YourContentPage : Page, IAppStateAware 
{ 
    public Task OnResumeApplicationAsync() 
    { 
     throw new System.NotImplementedException(); 
    } 

    public Task OnSleepApplicationAsync() 
    { 
     throw new System.NotImplementedException(); 
    } 

    public Task OnStartApplicationAsync() 
    { 
     throw new System.NotImplementedException(); 
    } 
} 

public interface IAppStateAware 
{ 
    Task OnResumeApplicationAsync(); 
    Task OnSleepApplicationAsync(); 
    Task OnStartApplicationAsync(); 
} 
0

正如josemigallas說,您正在尋找App.OnResume()。您可以輕鬆地訂閱和退訂一個MessagingCenter事件可以從您的App.OnResume()方法發送到觸發每個訂閱您ContentPage S的動作(不要忘了從事件中ContentPage.OnDisappearing()退訂防止內存泄漏):

在你ContentPage S:

protected override void OnAppearing() { 
    Debug.WriteLine("OnAppearing()"); 
    base.OnAppearing(); 

    MessagingCenter.Subscribe<App>(this, "SpecialOnResumeKeyValue", app => { 
      //Do something 
    }; 
} 

protected override void OnDisappearing() { 
    Debug.WriteLine("OnDisappearing"); 
    base.OnDisappearing(); 

    MessagingCenter.Unsubscribe<App>(this, "SpecialOnResumeKeyValue"); //VERY IMPORTANT 
} 

然後在您的App.xaml.cs

protected override async void OnResume() { 
    MessagingCenter.Send(this, "SpecialOnResumeKeyValue"); 
}