更新隔离存储中的数据

时间:2012-04-04 02:12:28

标签: windows-phone-7 c#-4.0

我有一个代码可以在隔离空间中添加电子邮件ID和名称。但它无法添加多个数据。另外,如果输入的数据不正确,我该如何更新?

namespace IsoStore
{

    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings.Add("email", "someone@somewhere.com");
            IsolatedStorageSettings.ApplicationSettings.Add("name", "myname");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBlock1.Text = (string)IsolatedStorageSettings.ApplicationSettings["email"];
            textBlock2.Text = (string)IsolatedStorageSettings.ApplicationSettings["name"];
        }
    }
}

2 个答案:

答案 0 :(得分:3)

使用帮助程序方法为您做一点清理代码:

namespace IsoStore
{
    public partial class MainPage : PhoneApplicationPage
    {
        private IsolatedStorageSettings _appSettings;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            _appSettings = IsolatedStorageSettings.ApplicationSettings;                
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SaveSetting("email", "someone@somewhere.com");
            SaveSetting("name", "myname");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBlock1.Text = (string)_appSettings["email"];
            textBlock2.Text = (string)_appSettings["name"];
        }

        private void SaveSetting( string setting, string value )
        {
            if (_appSettings.Contains(setting))
            {
                _appSettings[setting] = value;
            }
            else
            {
                _appSettings.Add(setting, value);
            }
        }
    }
}

尝试使用其他一些示例来了解使用IsolatedStorageSettings。

答案 1 :(得分:1)

我想到了2个选项,您要么将数据保存到isolatedStorageFile MSDN Library或者,这就是我在这种情况下可能会做的事情,您在密钥电子邮件下保存所有电子邮件作为一个字符串将电子邮件与一个不允许在电子邮件中使用的字符,Coma“,”可以说,在需要时拆分你的字符串并将其检索到任何让你感到舒服的东西。

private void SaveSetting( string setting, string value )
    {
        if (_appSettings.Contains(setting))
        {
            _appSettings[settings] = _appSettings[settings] + "," + value;
        }
        else
        {
            _appSettings.Add(setting, value);
        }
    }

请注意,此代码段是从HiTech Magic的回答中复制的。