Joe*_*ehl 27 c# events page-lifecycle xamarin xamarin.forms
我刚刚开发了我的第一个xamarin.forms应用程序.我对xamarin.forms很兴奋,但我想念几个事件.
在xamarin.forms ContentPage中是否有任何页面生命周期事件?
我知道这两个:
protected override void OnAppearing()
{
}
protected override void OnDisappearing()
{
}
Run Code Online (Sandbox Code Playgroud)
但OnAppearing()事件只触发一次.在Android上,当我按下开始按钮并返回到我的应用程序时,此事件不会再次触发.
是否有解决方法(如WindowsPhone页面中的OnNavigatedTo)?
谢谢.
use*_*er1 27
因此,OnAppearing当您的页面出现时会触发该事件.即,您已导航到该页面或从堆栈中的另一个页面返回该页面.Page Lifecycle您可以从API文档中看到目前没有任何事件
我认为你所说的是如果你让你的应用程序进入睡眠状态并导航回它,OnAppearing事件不会被触发,这是因为你的页面没有出现,因为它已经存在,应用程序只是睡着了.
您正在寻找的是App Lifecycle包括以下方法的方法:
protected override void OnStart()
{
Debug.WriteLine ("OnStart");
}
protected override void OnSleep()
{
Debug.WriteLine ("OnSleep");
}
protected override void OnResume()
{
Debug.WriteLine ("OnResume");
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用该OnResume事件来执行您要查找的内容.
该Xamarin文档包含必须对旧的Xamarin项目进行的更改以访问这些事件.如您App在您的共享库类必须继承Xamarin.Forms.Application和变化也必须对制造AppDelegate和MainActivity.
您还可以使用Xamarin的MessagingCenter来执行任意事件协调:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/messaging-center/
我对同样的事情感到沮丧:在Xamarin.Forms中没有一致的视图生命周期事件.但是,您可以使用MessagingCenter来解决其中一些限制,MessagingCenter只是一个简单的发布/订阅消息传递工具.
这对我有用.Xamarin.Forms 2.0及以上版本.每当您返回此CP页面时,Appearing都会触发事件.
public CP:ContentPage
{
//....
public CP()
{
this.Appearing += CP_Appearing ;
//...
}
private void CP_Appearing(object sender, EventArgs e)
{
Debug.WriteLine("*************HALLO******WELCOME BACK.");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40911 次 |
| 最近记录: |