NEW FEATURE: Navigation Containers now support View-specific Navigation State

67 views
Skip to first unread message

Rishi Oberoi

unread,
Jan 24, 2011, 11:53:17 PM1/24/11
to nro...@googlegroups.com
One of the things you see in highly polished navigation-driven applications (like in WP7) is that they hold the visual state as you navigate back and forward. Now, one way of doing that is to hold the entire previous/next visuals in memory or the other (better/intelligent) way is to cache only the relevant visual cues and store/restore them as you navigate. The later approach is now supported in nRoute, with the addition of the ISupportNavigationViewState contract. This new contract actually looks quite similar to what you might be used to:
    public interface ISupportNavigationViewState 
    {
        void RestoreState(ParametersCollection state);

        ParametersCollection SaveState();     }
Yes, for those in the know, it reads exactly the same as the ISupportNavigationState interface; however this is specifically meant to cache the visuals related state as opposed to the logical state. The idea is that you'll implement this off the View, and store things like the scroll-position or selected index and restore them when back/forward/return-navigated upon, consider:
        public void RestoreState(ParametersCollection state)
        {
            this.SearchTextbox.Text = state.GetValueOrDefault("SEARCH"this.SearchTextbox.Text);

            var _offset = default(double);
            if (state.TryGetValue("VOFFSET"out _offset))
            {
                ScrollView.Loaded += (s, e) => ScrollView.ScrollToVerticalOffset(_offset);
            }
        }

        public ParametersCollection SaveState()
        {
            return new ParametersCollection()
            {
                new Parameter("SEARCH", SearchTextbox.Text),
                new Parameter("VOFFSET", ScrollView.VerticalOffset)
            };
        }
And so now all the ISupportNavigationViewState supporting navigation containers keep two separate states - one for the logical state (normally from the VM) and one for the visual state (normally from the View). I think this will allow us to create more engaging and visually consistent user-experiences - or so I think :)

Cheers,
Rishi

PS: Note the GetValueOrDefault and TryGetValue methods off the state parameter, they are two of the newly added extension methods which hopefully will make it easier to get around the non-strongly typed nature of the ParametersCollection type. 
Reply all
Reply to author
Forward
0 new messages