xamarin.forms中的页面生命周期事件 [英] Page lifecycle events in xamarin.forms
问题描述
在xamarin.forms ContentPage中是否有任何页面生命周期事件?
我知道这两个:
protected override void OnAppearing()
{
}
protected override void OnDisappearing()
{
}
但是OnAppearing()事件只会触发一次。在Android上,当我按下开始按钮并返回到我的应用程序时,此事件不会再次触发。
是否有解决方法(如Windows Phone中的OnNavigatedTo)页)?
谢谢。
所以当您的页面出现时,OnAppearing
事件触发。即您已经导航到该页面或从堆栈中的另一个页面返回到该页面。目前没有页面生命周期事件。页面%2F *> API文档
我想你正在谈论的是如果你把你的应用程序睡觉并导航回到code> OnAppearing 事件没有被触发,这是因为你的页面没有出现,因为它已经在那里,应用程序刚刚入睡。
您正在寻找的是 应用程序生命周期
,其中包括以下方法:
protected override void OnStart()
{
Debug.WriteLine(OnStart);
}
protected override void OnSleep()
{
Debug.WriteLine(OnSleep);
}
protected override void OnResume()
{
Debug.WriteLine(OnResume);
}
然后可以使用 OnResume
事件来做你想要的。
Xamarin文档包括必须对旧的Xamarin项目进行这些事件的更改。例如您的共享库中的应用程序
类必须从 Xamarin.Forms.Application
继承,并且还必须对 AppDelegate
和 MainActivity
。
I just developed my first xamarin.forms app. I am excited about xamarin.forms, but I miss several events.
Are there any page-lifecycle-events in a xamarin.forms ContentPage?
I know of these two:
protected override void OnAppearing()
{
}
protected override void OnDisappearing()
{
}
But the OnAppearing() event only fires once. On Android, when I push the start button and go back to my app, this event does not fire again.
Is there a workaround for this (like OnNavigatedTo in WindowsPhone-pages)?
Thanks.
So the OnAppearing
event is fired when your page is appearing. I.e. you have navigated to that page or back to that page from another in the Stack. There is currently no Page Lifecycle
events as you can see from the API documentation
I think what you are talking about is if you put your application to sleep and navigate back into it the OnAppearing
event is not fired, This is because your page hasn't appeared because it was already there, the application was just asleep.
What you are looking for is the App Lifecycle
which includes methods such as:
protected override void OnStart()
{
Debug.WriteLine ("OnStart");
}
protected override void OnSleep()
{
Debug.WriteLine ("OnSleep");
}
protected override void OnResume()
{
Debug.WriteLine ("OnResume");
}
You can then use the OnResume
event to do what you are looking for.
That Xamarin Document includes the changes that must be made to old Xamarin projects to access these events. e.g. your App
class in your shared library must inherit from Xamarin.Forms.Application
and changes must also be made to the AppDelegate
and MainActivity
.
这篇关于xamarin.forms中的页面生命周期事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!