首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字典TryGetValue

字典TryGetValue
EN

Stack Overflow用户
提问于 2013-12-04 08:34:49
回答 1查看 2.4K关注 0票数 1

当用户离开我的应用程序时,我想在windows中保留状态。

如何创建从字典中获取值的通用方法TryGetValue

到目前为止我的代码是:

代码语言:javascript
复制
    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
    }

我想像这样打电话。

代码语言:javascript
复制
    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);
    }

我找不到有效的样本。

EN

回答 1

Stack Overflow用户

发布于 2013-12-04 08:37:26

下面是我使用的IDictionary的扩展方法:

代码语言:javascript
复制
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;
}

用法:

代码语言:javascript
复制
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");

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

要在不使用扩展方法的情况下实现所需的目标,只需使用该方法的主体:

代码语言:javascript
复制
string temp;
string two = dict.TryGetValue(2, out temp) ? temp : "two";
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20370540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档