簡體   English   中英

如何管理應用狀態

[英]How to manage App state

說我以這種方式設計了頁面導航:

P(1)->轉到P(2)-> goto P(3)並在P(3)上,用戶單擊“主頁”按鈕(Microsoft按鈕)

a)重新啟動應用程序后,如何返回到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();
        }

您可以使用本地設置來存儲每個頁面的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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM