【问题标题】:Window.Current.Content set with a frame but other pages do not navigateWindow.Current.Content 设置有框架但其他页面不导航
【发布时间】:2012-12-25 05:08:57
【问题描述】:

我遇到了 App.Xaml.cs 具有以下代码的问题:

        var gamePage = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (gamePage == null)
        {
            // Create a main GamePage
            gamePage = new Frame();

            if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                // TODO: Load state from previously suspended application
            }


            // Place the GamePage in the current Window
            Window.Current.Content = gamePage;
            gamePage.Navigate(typeof(MainMenu));

        }

        // Ensure the current window is active
        Window.Current.Activate();

这对于应用初始化来说是相当标准的。这也与 Microsoft 概述您应该这样做的方式相同:

  http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh771188.aspx

但是,当我再次调用框架时(在这种情况下是在 OnNavigateTo 中,因为现在我想确保我可以切换页面,这将在稍后移动)它什么也不做并且位于同一页面上.换句话说如下:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        var gamePage = Window.Current.Content as Frame;
        gamePage.Navigate(typeof(GamePage));

    }

但是,我不能像 Microsoft 推荐的那样调用 this.Frame,这可能是也可能不是问题。

所以,回顾一下,问题是我无法使用加载到 Window.Current.Content 中的标准 Frame 对象从一个页面导航到另一个页面。我可以验证该应用程序确实进入了 OnNavigateTo 并且第一页切换工作正常。但是当我再次尝试切换页面时,它不再起作用了。

有什么想法吗?非常感谢任何帮助!

【问题讨论】:

    标签: c# xaml windows-8


    【解决方案1】:

    gamePad 设为 App.xaml.cs 类的属性 (public static Frame gamePage = new Frame();),然后执行以下操作:

     var gamePad = App.gamePage;
    

    在其他页面中。这就是使用 MVVM 进行导航的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多