有没有一种方法可以防止WPF页面被浏览掉? [英] Is there a way to prevent that a WPF page was navigated away?
问题描述
我正在构建带有页面的WPF导航应用程序. 我想问用户离开页面时是否要保存更改.
I'm building a WPF navigation app, with pages. I want to ask the user if he want to save the changes when he leaves a page.
有办法吗?
推荐答案
注册NavigationService.Navigating
事件.完成操作后,别忘了删除您的注册,因为NavigationService
基本上会永远存在.
Register for the NavigationService.Navigating
event. Don't forget to remove your registration when you are done, since NavigationService
sticks around basically forever.
当您获得导航"事件并进行更改时,请显示一个MessageBox以便与用户确认或让他们有机会保存其数据.如果他们单击取消",则将e.Cancel = true
设置为取消导航.
When you get the Navigating event and there are changes, show a MessageBox to confirm with the user or give them a chance to save their data. If they hit Cancel, set e.Cancel = true
to cancel the navigation.
这篇关于有没有一种方法可以防止WPF页面被浏览掉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!