3

我有一个在独立空间中添加电子邮件 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"];
        }
    }
}
4

2 回答 2

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。

于 2012-04-04T08:13:56.393 回答
1

我想到了 2 个选项,您可以将数据保存到 isolatedStorageFile MSDN 库,或者,在这种情况下,我可能会这样做,您将所有电子邮件保存在密钥电子邮件下作为一个字符串,用不允许的字符分隔电子邮件在电子邮件中,昏迷“,”可以说,当需要时拆分您的字符串并将其检索到任何让您感到舒服的地方。

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

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

于 2012-04-06T04:33:56.770 回答