mhmad
mhmad

Reputation: 43

Remove touch handler when changing pages

I have a Windows Phone 7 project with has two pages (home page, about page). I have implemented the touch event handler on the home page containing some code. When I redirect to the about page and touch on this page, the code in the touch handler in the home page executes.

How I can prevent this handler on the about page?

Upvotes: 0

Views: 324

Answers (1)

Alaa Masoud
Alaa Masoud

Reputation: 7135

System.Windows.Input.Touch.FrameReported is a static event that will affect all your pages. You need to unsubscribe from it in the pages where you don't want it to be called.

To do that you'll need a reference to your main page so that you can unsubscribe this event from other pages. What I would do is add a static variable in App.cs that holds a reference to your main page and also make your method public. Set the reference in the constructor of MainPage.

// Add this field in App class in App.cs
public static MainPage MainPage { get; set; }
// MainPage Ctor
public MainPage() {
  App.MainPage = this;
  //...
}

Then on the NavigatedTo event of your other pages you would just unsubscribe the touch event

// In about page
protected override void OnNavigatedTo(NavigationEventArgs e) {
  base.OnNavigatedTo(e);
  System.Windows.Input.Touch.FrameReported -= App.MainPage.Touch_FrameReported;
}

You could also consider using WP Toolkit's gesture listener if you want to listen to gestures on specific elements.

Upvotes: 1

Related Questions