开发者

WP7 pivotcontrol remember selected item

开发者 https://www.devze.com 2023-01-26 08:36 出处:网络
On windows phone 开发者_Python百科7 pivot control template project, the pivot control page doesn\'t remember the selected item if you go to the search page from a specific pivot item and select back o

On windows phone 开发者_Python百科7 pivot control template project, the pivot control page doesn't remember the selected item if you go to the search page from a specific pivot item and select back on the phone. It always comes back to the first item of the pivot control.

How do you change this behavior so if you were on the 3rd pivot item and you go to Search and hit back, you come back to the 3rd pivot item.

Pratik


When you press Search button your app is tombstoned(in other words app is stopped and kept in memory as long it is possible).It is completely up to you (developer) how would handle it. The system itself does only a few things to get the last back state - like navigate to the last page. You can think of it like cookies in browser. If you press back button browser will check if a cookie exists and load the content with info from the cookie.

There are several ways to handle it and give user the best UX. You can save the state to the State collection or directly to IsolatedStorage. Use event in App.xaml.cs

    // Code to execute when the application is launching (eg, from Start)
    // This code will not execute when the application is reactivated
    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
    }

    // Code to execute when the application is activated (brought to foreground)
    // This code will not execute when the application is first launched
    private void Application_Activated(object sender, ActivatedEventArgs e)
    {

    }

    // Code to execute when the application is deactivated (sent to background)
    // This code will not execute when the application is closing
    private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
    }

    // Code to execute when the application is closing (eg, user hit Back)
    // This code will not execute when the application is deactivated
    private void Application_Closing(object sender, ClosingEventArgs e)
    {
    }

or events for your page with a pivot

    // set state
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
#if DEBUG
        Debug.WriteLine("TOMBSTONING EVENT: OnNavigatedFrom at {0}", DateTime.Now.ToLongTimeString());
#endif
        //try to locate state if exists
        if (State.ContainsKey(App.STATE_KEY))
        {
            //clear prev value
            State.Remove(App.STATE_KEY);
        }
        State.Add(App.STATE_KEY, this.State);
        base.OnNavigatedFrom(e);
    }

    // get state
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        // try to locate the state from previous run
        if (State.ContainsKey(App.STATE_KEY))
        {
            // return previous state
            var s = State[App.STATE_KEY] as Info;
            if (s != null)
            {
#if DEBUG
                Debug.WriteLine("TOMBSTONING EVENT: OnNavigatedTo at {0}", DateTime.Now.ToLongTimeString());
#endif
                this.State = s;
            }

        }

        base.OnNavigatedTo(e);
    }

use this pattern for your page with the pivot and save the last index of your pivot control. try and catch blocks would be nice as well.

Overview Lifecycle <- a movie

0

精彩评论

暂无评论...
验证码 换一张
取 消