Answered by:
How to forbid the Back button function in a page of windows phone 8.1 app?

Question
-
I do not want the user presses Back Button to back to the previous page while the data syncing is in progress.
I have tried the following code, but it does not work:
public SettingPage() { this.InitializeComponent(); this.navigationHelper = new NavigationHelper(this); this.navigationHelper.LoadState += this.NavigationHelper_LoadState; this.navigationHelper.SaveState += this.NavigationHelper_SaveState; Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed; }
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) { if (this.isSyncing) { e.Handled = true; } }
Thursday, November 6, 2014 10:28 AM
Answers
-
Because the NavigationHelper will process this event first. you need to modify this behavior.
you can see the code in NavigationHelper.cs (it's constructor)
public NavigationHelper(Page page) { this.Page = page; // When this page is part of the visual tree make two changes: // 1) Map application view state to visual state for the page // 2) Handle hardware navigation requests this.Page.Loaded += (sender, e) => { #if WINDOWS_PHONE_APP Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed; #else
#if WINDOWS_PHONE_APP /// <summary> /// Invoked when the hardware back button is pressed. For Windows Phone only. /// </summary> /// <param name="sender">Instance that triggered the event.</param> /// <param name="e">Event data describing the conditions that led to the event.</param> private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) { if (this.GoBackCommand.CanExecute(null)) { e.Handled = true; this.GoBackCommand.Execute(null); } }
在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。
- Edited by Bill ChungMVP Thursday, November 6, 2014 4:05 PM
- Proposed as answer by Herro wongMicrosoft contingent staff, Moderator Monday, November 10, 2014 9:30 AM
- Marked as answer by Herro wongMicrosoft contingent staff, Moderator Monday, November 17, 2014 9:56 AM
Thursday, November 6, 2014 4:02 PM
All replies
-
Have you check isSyncing are true?
This code is right, but you need to check and using debug point to check, wether e.Handled is executed.
Thursday, November 6, 2014 2:34 PM -
Because the NavigationHelper will process this event first. you need to modify this behavior.
you can see the code in NavigationHelper.cs (it's constructor)
public NavigationHelper(Page page) { this.Page = page; // When this page is part of the visual tree make two changes: // 1) Map application view state to visual state for the page // 2) Handle hardware navigation requests this.Page.Loaded += (sender, e) => { #if WINDOWS_PHONE_APP Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed; #else
#if WINDOWS_PHONE_APP /// <summary> /// Invoked when the hardware back button is pressed. For Windows Phone only. /// </summary> /// <param name="sender">Instance that triggered the event.</param> /// <param name="e">Event data describing the conditions that led to the event.</param> private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) { if (this.GoBackCommand.CanExecute(null)) { e.Handled = true; this.GoBackCommand.Execute(null); } }
在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。
- Edited by Bill ChungMVP Thursday, November 6, 2014 4:05 PM
- Proposed as answer by Herro wongMicrosoft contingent staff, Moderator Monday, November 10, 2014 9:30 AM
- Marked as answer by Herro wongMicrosoft contingent staff, Moderator Monday, November 17, 2014 9:56 AM
Thursday, November 6, 2014 4:02 PM