字典尝试获取值

use*_*164 2 c# windows-phone-7 windows-phone-8

我想在用户离开我的应用程序时保留 Windows Phone 中的状态。
如何创建一个TryGetValue从字典中获取值的通用方法?

到目前为止我的代码:

    public class StatefulPhoneApplication : PhoneApplicationPage
    {
        #region constructor
        public StatefulPhoneApplication()
        {
            IsNewPageInstance = true;
        }
        #endregion

        #region properties
        protected bool IsNewPageInstance { get; private set; }
        protected bool IsStatePreserved
        {
            get
            {
                if (this.State.ContainsKey("StatePreserved"))
                    return (bool)this.State["StatePreserved"];
                else
                    return false;
            }
        }
        #endregion

        #region preservation methods
        protected void PreserveControlState(Control control)
        {
            if (control is TextBox)
                PreserveTextBoxState(control as TextBox);
            else
                PreserveCheckBoxState(control as CheckBox);

            this.State["StatePreserved"] = true;
        }

        protected void PreserveTextBoxState(TextBox textBox)
        {
            this.State[textBox.Name + ".Text"] = textBox.Text;
            this.State[textBox.Name + ".SelectionStart"] = textBox.SelectionStart;
            this.State[textBox.Name + ".SelectionLength"] = textBox.SelectionLength;
        }

        protected void PreserveCheckBoxState(CheckBox checkBox)
        {
            this.State[checkBox.Name + ".IsChecked"] = checkBox.IsChecked;
        }

        protected void PreserveFocusState(FrameworkElement parent)
        {
            Control focusedControl = FocusManager.GetFocusedElement() as Control;

            if (focusedControl == null)
            {
                this.State["FocusedControlName"] = null;
            }
            else
            {
                Control controlWithFocus = parent.FindName(focusedControl.Name) as Control;

                if (controlWithFocus == null)
                {
                    this.State["FocusedElementName"] = null;
                }
                else
                {
                    this.State["FocusedElementName"] = focusedControl.Name;
                }
            }   

        }
        #endregion

        #region restoration methods
        private void RestoreControlState(Control control)
        {
            if (control is TextBox)
                RestoreTextBoxState(control as TextBox, string.Empty);
            else if (control is CheckBox)
                RestoreCheckBoxState(control as CheckBox, false);
        }
        #endregion
    }
Run Code Online (Sandbox Code Playgroud)

我想就这样打电话。

    private void RestoreTextBoxState(TextBox textBox, string defaultValue)
    {
        textBox.Text = TryGetValue<string>(textBox.Name + ".Text", defaultValue);
        textBox.SelectionStart = TryGetValue<int>(textBox.Name + ".SelectionStart", defaultValue);
        textBox.SelectionLength = TryGetValue<int>(textBox.Name + ".SelectionLength", defaultValue);
    }
Run Code Online (Sandbox Code Playgroud)

我找不到有效的好样本。

Bri*_*ntz 6

这是我使用的 IDictionary 的扩展方法:

public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue @default = default(TValue)) {
    if (@this == null) return @default;

    TValue value;

    return @this.TryGetValue(key, out value) ? value : @default;
}
Run Code Online (Sandbox Code Playgroud)

用法:

Dictionary<int, string> dict = new Dictionary<int, string>() {
    { 1, "one" },
    { 3, "three" }
};

string one = dict.GetValueOrDefault(1, "one");
string two = dict.GetValueOrDefault(2, "two");
string three = dict.GetValueOrDefault(3, "three");
Run Code Online (Sandbox Code Playgroud)

在上面的代码之后,onetwothree都将被设置为正确的字符串值,即使字典中没有该键的条目2


要在不使用扩展方法的情况下实现您想要的效果,您可以仅使用方法的主体:

string temp;
string two = dict.TryGetValue(2, out temp) ? temp : "two";
Run Code Online (Sandbox Code Playgroud)