Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

c# - Page-based action for back button

My question is how to define own action for given page i.e. first click on back button close popup and second navigates from page?

Since WP8.1 NavigationHelper is quite useful yet it only gives one general action for clicking back key (exiting page).

I can't overwrite NavigationHelper since it gives no setters for its commands and handler for back button click is private, so I can't unpin it on entering page. Making changes in NavigationHelper seems ugly since I'm developing universal app for W8.1 & WP8.1 and I have multiple pages that require custom handler for back button.

--edit--

Commands allows overwriting, yet I'll use original command on each page. Solution is to create new command on each page?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you can subscribe to Windows.Phone.UI.Input.HardwareButtons.BackPressed before the NavigationHelper does (as I've checked it subscribes in Page.Loaded event). In fact for this purpose (as you will be adding EventHandlers later) you will need a special InvokingMethod that will invoke your EventHandlers:

// in App.xaml.cs make a method and a listOfHandlers:
private List<EventHandler<BackPressedEventArgs>> listOfHandlers = new List<EventHandler<BackPressedEventArgs>>();

private void InvokingMethod(object sender, BackPressedEventArgs e)
{
   for (int i = 0; i < listOfHandlers.Count; i++)
       listOfHandlers[i](sender, e);
}

public event EventHandler<BackPressedEventArgs> myBackKeyEvent
{
   add { listOfHandlers.Add(value); }
   remove { listOfHandlers.Remove(value); }
}

// look that as it is a collection you can make a variety of things with it
// for example provide a method that will put your new event on top of it
// it will beinvoked as first
public void AddToTop(EventHandler<BackPressedEventArgs> eventToAdd) { listOfHandlers.Insert(0, eventToAdd); }

// in App constructor: - do this as FIRST eventhandler subscribed!
HardwareButtons.BackPressed += InvokingMethod;

// subscription:
(App.Current as App).myBackKeyEvent += MyClosingPopupHandler;
// or
(App.Current as App).AddToTop(MyClosingPopupHandler); // it should be fired first

// also you will need to change in NavigationHelper.cs behaviour of HardwereButtons_BackPressed
// so that it won't fire while e.Handeled == true
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (!e.Handled)
    {
        // rest of the code
    }
}

In this case BackPressed will be fired before NavigationHelper's eventhandler and if you set e.Handeled = true; then you should stay on the same Page.

Note also that instead of modifying App.xaml.cs you can extend this way your Page class or NavigationHelper, it depends on your needs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...