Refreshing data in isolated storage

I have some code that adds an email id and name in an isolated space. But he cannot add multiple data. Also, how can I update if any data was entered incorrectly?

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"];
        }
    }
}

      

+3


source to share


2 answers


You cleaned up your code a bit by using a storage helper method:

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);
            }
        }
    }
}

      



Try some more examples to get your head around with IsolatedStorageSettings.

+3


source


I mean 2 options, you either save your data in the isolStorageFile MSDN Library OR this is what I can do in such a case, you save all your emails to the key email address as one line separates emails with char which cannot be in the email address, Coma "," let's say when you need to split your string and get it to whatever is convenient for you,

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

      



note that this code segment is copied from HiTech Magic's answer.

+1


source







All Articles