Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于页面的后退按钮操作_C#_Windows Phone 8.1_Win Universal App - Fatal编程技术网

C# 基于页面的后退按钮操作

C# 基于页面的后退按钮操作,c#,windows-phone-8.1,win-universal-app,C#,Windows Phone 8.1,Win Universal App,我的问题是如何为给定页面定义自己的操作,即第一次单击后退按钮关闭弹出窗口,第二次从页面导航 由于WP8.1 NavigationHelper非常有用,但它只提供了一个单击后退键(退出页面)的常规操作 我不能覆盖NavigationHelper,因为它没有为其命令提供setter,后退按钮单击的处理程序是私有的,所以我不能在进入页面时取消锁定它。在NavigationHelper中进行更改看起来很难看,因为我正在为W8.1和WP8.1开发通用应用程序,并且我有多个页面需要自定义返回按钮处理程序 -

我的问题是如何为给定页面定义自己的操作,即第一次单击后退按钮关闭弹出窗口,第二次从页面导航

由于WP8.1 NavigationHelper非常有用,但它只提供了一个单击后退键(退出页面)的常规操作

我不能覆盖NavigationHelper,因为它没有为其命令提供setter,后退按钮单击的处理程序是私有的,所以我不能在进入页面时取消锁定它。在NavigationHelper中进行更改看起来很难看,因为我正在为W8.1和WP8.1开发通用应用程序,并且我有多个页面需要自定义返回按钮处理程序

--编辑--


命令允许覆盖,但我将在每个页面上使用原始命令。解决方案是在每个页面上创建新命令?

我认为您可以在NavigationHelper之前订阅
Windows.Phone.UI.Input.HardwareButtons.BackPressed
(正如我在page.Loaded event中检查的那样)。事实上,出于此目的(稍后将添加EventHandler),您将需要一个特殊的调用方法来调用您的EventHandler:

// 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
    }
}
//在App.xaml.cs中创建方法和句柄列表:
私有列表listofHandler=新列表();
私有void调用方法(对象发送方,BackPressedEventArgs e)
{
for(int i=0;i
在这种情况下,BackPressed将在NavigationHelper的eventhandler之前触发,如果您设置
e.handled=true然后你应该保持在同一页上


还请注意,您可以通过这种方式扩展页面类或NavigationHelper,而不是修改App.xaml.cs,这取决于您的需要。

取决于事件的添加顺序非常难看。使用NavigationHelper比绕过它要干净得多

您可以将自己的RelayCommand设置为NavigationHelper的GoBackCommand和GoForwardCommand属性。另一个选项是将NavigationHelper子类化,并覆盖CanGoBack和GoBack函数(它们是虚拟的)


无论采用哪种方式,您都可以使用自定义逻辑替换NavigationHelper的默认操作,以关闭任何中间状态或调用Frame.GoBack(视情况而定)。

+1正确的、更干净的解决方案-