简体   繁体   English

Xamarin.forms离开/更改页面时的事件

[英]Xamarin.forms event on leaving/changing the page

I'm looking for something like a function 我正在寻找像功能一样的东西

protected override void OnDisappearing()
{
}

in Xamarin Forms. 在Xamarin表格中。 The problem is that this feature is called even when I turn off the phone display. 问题是即使关闭手机显示屏也会调用此功能。 I'm looking for a feature that only responds to the event when the page is changed - for example, the user in the menu clicks on a page other than the one that is being visited. 我正在寻找一个仅在页面更改时响应事件的功能 - 例如,菜单中的用户单击正在访问的页面以外的页面。

thanks for your advice 谢谢你的建议

You will need to combine OnBackButtonPressed and OnDisappearing 您需要结合OnBackButtonPressedOnDisappearing

OR OnAppearing and OnDisappearing OnAppearingOnDisappearing

You can create a base page which implement OnBackButtonPressed and or OnDisappearing and or OnAppearing . 您可以创建一个实现OnBackButtonPressed和/或OnDisappearing和/或OnAppearingbase页。

And you save "Active" page reference in a static variable. 并在静态变量中保存“活动”页面引用。

// this condition can help you concerning the screen on or off
if(this.Title == Activepage.Title) // or id
    return;

What MVVM framework are you using? 你使用什么MVVM框架? There are typically events built into the viewmodel that you can leverage. 通常,视图模型中内置了可以利用的事件。

If you want the page to do something specific on a button press then I'd suggest putting the code in there. 如果您希望页面按下按钮上的特定内容,那么我建议将代码放在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM