【问题标题】:How to manage App state如何管理应用状态
【发布时间】:2013-09-16 07:34:01
【问题描述】:

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

P(1) -> goto P(2) -> goto P(3) 并在 P(3) 处,用户单击 Home 按钮(Microsoft 按钮)

a) 应用重新启动时如何返回 p(3)?

谢谢

--- 更新

我需要在这个活动上做什么?

受保护的覆盖无效 OnLaunched(LaunchActivatedEventArgs args) { Frame rootFrame = Window.Current.Content as Frame; // 当 Window 已经有内容时,不要重复应用初始化, // 只需确保窗口处于活动状态 if (rootFrame == null) { // 创建一个 Frame 作为导航上下文并导航到第一页 rootFrame = new Frame(); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO:从先前挂起的应用程序加载状态 } // 将框架放置在当前窗口中 Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { // 当导航堆栈未恢复时,导航到第一页, // 通过传递所需信息作为导航来配置新页面 // 范围 if (!rootFrame.Navigate(typeof(MainPage), args.Arguments)) { throw new Exception("创建初始页面失败"); } } // 确保当前窗口处于活动状态 Window.Current.Activate(); }

【问题讨论】:

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


    【解决方案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();
    }
    

    【讨论】:

    • 如果我转到 P(2) 并按下 Home 按钮并重新启动,它将显示 p(2) 而不是 MainPage?如果我停在 P(3),它应该转到 P(3)。我需要做什么?
    • 我不明白你在说什么。主页按钮到底是什么意思?
    • 对不起。模拟器上的主页按钮(Microsoft 或中心按钮)。当应用程序运行时,按下它会进入主屏幕。
    • 看到 OnLaunched 只会在应用程序关闭或暂停时被调用,因此当应用程序未暂停时将难以导航,因为那时最后一页将处于打开状态该页面CoreWindow.Activated event 将被触发,因此您可以使用它。
    • 对不起,我之前关于中​​心按钮案例的问题不包括您更新的代码。现在,我按照您的操作进行操作,但出现错误: if (!rootFrame.Navigate(t, args.Arguments)) where t is null 并且我在 OnNavigated To 事件中包含了这一行:Windows.Storage.ApplicationData.Current .LocalSettings.Values["LastPage"] = this.GetType().ToString();
    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多