• <bdo id='mLTzy'></bdo><ul id='mLTzy'></ul>

      <small id='mLTzy'></small><noframes id='mLTzy'>

    1. <legend id='mLTzy'><style id='mLTzy'><dir id='mLTzy'><q id='mLTzy'></q></dir></style></legend>

      <i id='mLTzy'><tr id='mLTzy'><dt id='mLTzy'><q id='mLTzy'><span id='mLTzy'><b id='mLTzy'><form id='mLTzy'><ins id='mLTzy'></ins><ul id='mLTzy'></ul><sub id='mLTzy'></sub></form><legend id='mLTzy'></legend><bdo id='mLTzy'><pre id='mLTzy'><center id='mLTzy'></center></pre></bdo></b><th id='mLTzy'></th></span></q></dt></tr></i><div id='mLTzy'><tfoot id='mLTzy'></tfoot><dl id='mLTzy'><fieldset id='mLTzy'></fieldset></dl></div>
      <tfoot id='mLTzy'></tfoot>

        NavigationService 什么时候初始化?

        时间:2023-08-28
          <tbody id='JfZSy'></tbody>
      1. <tfoot id='JfZSy'></tfoot>

            • <bdo id='JfZSy'></bdo><ul id='JfZSy'></ul>

            • <i id='JfZSy'><tr id='JfZSy'><dt id='JfZSy'><q id='JfZSy'><span id='JfZSy'><b id='JfZSy'><form id='JfZSy'><ins id='JfZSy'></ins><ul id='JfZSy'></ul><sub id='JfZSy'></sub></form><legend id='JfZSy'></legend><bdo id='JfZSy'><pre id='JfZSy'><center id='JfZSy'></center></pre></bdo></b><th id='JfZSy'></th></span></q></dt></tr></i><div id='JfZSy'><tfoot id='JfZSy'></tfoot><dl id='JfZSy'><fieldset id='JfZSy'></fieldset></dl></div>

                • <legend id='JfZSy'><style id='JfZSy'><dir id='JfZSy'><q id='JfZSy'></q></dir></style></legend>

                  <small id='JfZSy'></small><noframes id='JfZSy'>

                  本文介绍了NavigationService 什么时候初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想从我的页面中捕获 NavigationService.Navigating 事件,以防止用户向前导航.我有一个这样定义的事件处理程序:

                  I want to catch the NavigationService.Navigating event from my Page, to prevent the user from navigating forward. I have an event handler defined thusly:

                  void PreventForwardNavigation(object sender, NavigatingCancelEventArgs e)
                  {
                      if (e.NavigationMode == NavigationMode.Forward)
                      {
                          e.Cancel = true;
                      }
                  }
                  

                  ... 效果很好.但是,我不确定该代码的确切位置:

                  ... and that works fine. However, I am unsure exactly where to place this code:

                  NavigationService.Navigating += PreventForwardNavigation;
                  

                  如果我将它放在页面的构造函数或 Initialized 事件处理程序中,则 NavigationService 仍然为 null,并且我得到 NullReferenceException.但是,如果我将它放在页面的 Loaded 事件处理程序中,那么每次导航到页面时都会调用它.如果我理解正确,这意味着我要多次处理同一个事件.

                  If I place it in the constructor of the page, or the Initialized event handler, then NavigationService is still null and I get a NullReferenceException. However, if I place it in the Loaded event handler for the Page, then it is called every time the page is navigated to. If I understand right, that means I'm handling the same event multiple times.

                  我可以多次向事件添加相同的处理程序吗(如果我使用页面的 Loaded 事件来连接它会发生这种情况)?如果没有,在 Initialized 和 Loaded 之间是否有一些地方可以进行这种接线?

                  Am I ok to add the same handler to the event multiple times (as would happen were I to use the page's Loaded event to hook it up)? If not, is there some place in between Initialized and Loaded where I can do this wiring?

                  推荐答案

                  NavigationService.Navigate 触发 NavigationService.Navigating 事件和 Application.Navigating事件.我用以下方法解决了这个问题:

                  NavigationService.Navigate triggers both a NavigationService.Navigating event AND an Application.Navigating event. I solved this problem with the following:

                  public class PageBase : Page
                  {
                      static PageBase()
                      {
                          Application.Current.Navigating += NavigationService_Navigating;
                      }
                  
                      protected static void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
                      {
                          // put your event handler code here...
                      }
                  }
                  

                  这篇关于NavigationService 什么时候初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <tfoot id='aMfO7'></tfoot>

                    <tbody id='aMfO7'></tbody>

                    • <bdo id='aMfO7'></bdo><ul id='aMfO7'></ul>
                    • <small id='aMfO7'></small><noframes id='aMfO7'>

                        <legend id='aMfO7'><style id='aMfO7'><dir id='aMfO7'><q id='aMfO7'></q></dir></style></legend>
                      1. <i id='aMfO7'><tr id='aMfO7'><dt id='aMfO7'><q id='aMfO7'><span id='aMfO7'><b id='aMfO7'><form id='aMfO7'><ins id='aMfO7'></ins><ul id='aMfO7'></ul><sub id='aMfO7'></sub></form><legend id='aMfO7'></legend><bdo id='aMfO7'><pre id='aMfO7'><center id='aMfO7'></center></pre></bdo></b><th id='aMfO7'></th></span></q></dt></tr></i><div id='aMfO7'><tfoot id='aMfO7'></tfoot><dl id='aMfO7'><fieldset id='aMfO7'></fieldset></dl></div>