C# 隔离存储不替换新值?应用程序中断?

C# 隔离存储不替换新值?应用程序中断?,c#,windows-phone-8,C#,Windows Phone 8,我使用IsolatedStorage从用户那里保存整数。要添加第一个整数,它工作得很好。已成功添加给定整数 但是,在使用IsolatedStorageIsolatedStorageSettings.ApplicationSettings[“Key”]=site给出另一个要保存在同一字符串上的整数(以替换旧整数)时,应用程序中断 这是我的代码: int num = 0; if (int.TryParse(txtbox.Text, out num) &&

我使用IsolatedStorage从用户那里保存整数。要添加第一个整数,它工作得很好。已成功添加给定整数

但是,在使用IsolatedStorage
IsolatedStorageSettings.ApplicationSettings[“Key”]=site给出另一个要保存在同一字符串上的整数(以替换旧整数)时,应用程序中断

这是我的代码:

        int num = 0;
        if (int.TryParse(txtbox.Text, out num) && num > 0)
        {
            string site;
            site = num.ToString();
            IsolatedStorageSettings.ApplicationSettings.Add("Key", site);
            IsolatedStorageSettings.ApplicationSettings["Key"] = site;
            IsolatedStorageSettings.ApplicationSettings.Save();
            MessageBox.Show("Bookmark created successfully");
        }
        else
        {
            MessageBox.Show("TextBox is not supposed to be empty");
        }

您应该删除此行:

IsolatedStorageSettings.ApplicationSettings.Add("Key", site);
IsolatedStorageSettings.ApplicationSettings["Key"] = site;
如果您已经有一个名为
Key
的设置,该行将抛出一个异常:

例外情况:
ArgumentException
-
已存在于字典中

。。。鉴于该行:

IsolatedStorageSettings.ApplicationSettings.Add("Key", site);
IsolatedStorageSettings.ApplicationSettings["Key"] = site;
只替换以前的任何值

不过,您应该退一步,找出自己无法发现这一点的原因:您说应用程序“中断”——可能引发了异常,并且您应该确保能够获得任何异常的堆栈跟踪(和消息)。这应该能找出问题所在。能够看到应用程序抛出的任何异常是非常重要的,否则诊断问题会非常困难