如何管理App状态

时间:2013-09-12 04:28:14

标签: c# windows-8 microsoft-metro windows-store-apps winrt-xaml

假设我以这种方式设计了页面导航:

P(1) - >转到P(2) - >转到P(3)并在P(3),用户单击主页按钮(Microsoft按钮)

a)当App重新启动时,如何回到p(3)?

由于

---更新

我需要对此活动做些什么?


protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active

            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page

                rootFrame = new Frame();

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

                // Place the frame in the current Window

                Window.Current.Content = rootFrame;

            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter

                if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
                {
                    throw new Exception("Failed to create initial page");
                }
            }

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



1 个答案:

答案 0 :(得分:1)

您可以使用本地设置存储在每页的OnNavigatedTo事件中打开的最后一页。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ApplicationData.Current.LocalSettings.Values["LastPage"] = this.GetType().ToString();
}

在App.xaml.cs的OnLaunched(..)事件之后,检查最后一页打开的是哪一个。根据你可以导航它。

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();

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

        // Place the frame in the current Window
        Window.Current.Content = rootFrame;
    }

    if (rootFrame.Content == null)
    {
        // When the navigation stack isn't restored navigate to the first page,
        // configuring the new page by passing required information as a navigation
        // parameter

        if (ApplicationData.Current.LocalSettings.Values["LastPage"] != null)
        {
            Type t = Type.GetType((string)ApplicationData.Current.LocalSettings.Values["LastPage"]);
            if (!rootFrame.Navigate(t, args.Arguments))
            {
                throw new Exception("Failed to create initial page");
            }
        }

        else
        {
            if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
            {
                throw new Exception("Failed to create initial page");
            }
        }
    }
    // Ensure the current window is active
    Window.Current.Activate();
}