Jos*_*osé 3 xamarin.ios xamarin xamarin.forms
我在iOS中的声明中遇到以下问题。当用户在后台返回到应用程序时,我需要激活一个命令。在我身上,ContentPage我找不到识别退货的方法。在Android中,它可以正常工作,仅在iOS中,我无法在内容中找到任何表明我要返回的函数
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();
}
Run Code Online (Sandbox Code Playgroud)
我尝试了三种选择,但没有结果
正如 josemigallas 所说,您正在寻找App.OnResume(). 您可以轻松订阅和取消订阅一个MessagingCenter事件,该事件可以从您的App.OnResume()方法发送以触发您ContentPage订阅的每个s 中的操作(不要忘记取消订阅其中的事件ContentPage.OnDisappearing()以防止内存泄漏):
在你ContentPage的:
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
}
Run Code Online (Sandbox Code Playgroud)
然后在您的App.xaml.cs:
protected override async void OnResume() {
MessagingCenter.Send(this, "SpecialOnResumeKeyValue");
}
Run Code Online (Sandbox Code Playgroud)
我通常以这种方式这样做,所以我不必打扰订阅:
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
891 次 |
| 最近记录: |