Window.Current.Content设置框架但其他页面不导航

时间:2012-12-24 07:01:46

标签: c# xaml windows-8

我遇到的问题是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();

这对于应用初始化来说是相当标准的。这也与微软概述你应该如何做的一样:

  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这样的框架建议我可能会或可能不会成为问题。

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

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

1 个答案:

答案 0 :(得分:0)

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

 var gamePad = App.gamePage;

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