Click here to Skip to main content
15,880,725 members
Articles / Mobile Apps / Windows Phone 7

A Windows Phone 7 App from the Ground Up

Rate me:
Please Sign up or sign in to vote.
4.95/5 (36 votes)
21 Dec 2010CPOL17 min read 82.2K   2.3K   85  
Building a WP7 browser app for last.fm
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;

namespace MyLastFm
{
    static class StateDictionaryExtenstions
    {
        public static void PreserveState(this IDictionary<string, object> state, Pivot pivot)
        {
            state[pivot.Name + "_SelectedIndex"] = pivot.SelectedIndex;
        }
        public static void RestoreState(this IDictionary<string, object> state, Pivot pivot)
        {
            int index = TryGetValue<int>(state, pivot.Name + "_SelectedIndex", -1);
            if (pivot.Items.Count >= index)
            {
                pivot.Dispatcher.BeginInvoke(() => pivot.SelectedIndex = index);                
            }
        }

        public static void PreserveState(this IDictionary<string, object> state, TextBox textBox)
        {
            state[textBox.Name + "_Text"] = textBox.Text;
            state[textBox.Name + "_SelectionStart"] = textBox.SelectionStart;
            state[textBox.Name + "_SelectionLength"] = textBox.SelectionLength;
        }
        public static void RestoreStatethis(IDictionary<string, object> state, TextBox textBox, string defaultValue)
        {
            textBox.Text = TryGetValue<string>(state, textBox.Name + "_Text", defaultValue);
            textBox.SelectionStart = TryGetValue<int>(state, textBox.Name + "_SelectionStart", textBox.Text.Length);
            textBox.SelectionLength = TryGetValue<int>(state, textBox.Name + "_SelectionLength", 0);
        }

        public static void PreserveState(this IDictionary<string, object> state, ScrollViewer scrollViewer)
        {
            state[scrollViewer.Name + "_VerticalOffset"] = scrollViewer.VerticalOffset;
        }
        public static void RestoreState(this IDictionary<string, object> state, ScrollViewer scrollViewer)
        {
            double offset = TryGetValue<double>(state, scrollViewer.Name + "_VerticalOffset", 0);
            if (offset > 0)
            {
                scrollViewer.Dispatcher.BeginInvoke(() => scrollViewer.ScrollToVerticalOffset(offset));
            }
        }

        public static void PreserveState(this IDictionary<string, object> state, WebBrowser webBrowser)
        {
            state[webBrowser.Name + "_Content"] = webBrowser.SaveToString();
        }
        public static void RestoreState(IDictionary<string, object> state, WebBrowser webBrowser)
        {
            string content = TryGetValue<string>(state, webBrowser.Name + "_Content", "");
            if (!string.IsNullOrEmpty(content))
            {
                webBrowser.Loaded += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e2)
                {
                    webBrowser.NavigateToString(content);
                });
            }
        }

        public static void PreserveFocusState(this IDictionary<string, object> state, FrameworkElement parent)
        {
            // Determine which control currently has focus.
            Control focusedControl = FocusManager.GetFocusedElement() as Control;

            // If no control has focus, store null in the State dictionary.
            if (focusedControl == null)
            {
                state["FocusedControlName"] = null;
            }
            else
            {
                // Find the control within the parent
                Control foundFE = parent.FindName(focusedControl.Name) as Control;

                // If the control isn't found within the parent, store null in the State dictionary.
                if (foundFE == null)
                {
                    state["FocusedElementName"] = null;
                }
                else
                {
                    // otherwise store the name of the control with focus.
                    state["FocusedElementName"] = focusedControl.Name;
                }
            }
        }
        public static void RestoreFocusState(this IDictionary<string, object> state, FrameworkElement parent)
        {
            // Get the name of the control that should have focus.
            string focusedName = TryGetValue<string>(state, "FocusedElementName", null);

            // Check to see if the name is null or empty
            if (!String.IsNullOrEmpty(focusedName))
            {

                // Find the control name in the parent.
                Control focusedControl = parent.FindName(focusedName) as Control;
                if (focusedControl != null)
                {
                    // If the control is found, call its Focus method.
                    parent.Dispatcher.BeginInvoke(() => { focusedControl.Focus(); });
                }
            }
        }

        public static T TryGetValue<T>(this IDictionary<string, object> state, string name, T defaultValue)
        {
            if (state.ContainsKey(name))
            {
                if (state[name] != null)
                {
                    return (T)state[name];
                }
            }
            return defaultValue;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions