Question

I have one problem which I cannot resolve (at least I didn't found solution yet...) I wanted to add data into IsolatedStorageSettings in WP8. My goal is to check if there are settings already in there, but when I want to check I get an exception which I cannot resolve.

    private IsolatedStorageSettings appSettings = new IsolatedStorageSettings();

                private void Button_Click(object sender, RoutedEventArgs e)
    {

        //correct this part
        if (String.IsNullOrEmpty((string)appSettings["ICE1"])|| 
            String.IsNullOrEmpty((string)appSettings["ICE2"]) || 
            String.IsNullOrEmpty((string)appSettings["ICE3"]) || 
            String.IsNullOrEmpty((string)appSettings["address"]) || 
            String.IsNullOrEmpty((string)appSettings["fullName"]))
        {
        appSettings.Add("fullName", fullName.Text);
        appSettings.Save();

        appSettings.Add("address", address.Text);
        appSettings.Save();
        if (Regex.IsMatch(prefix1.Text, "^+\\d{2,3}") && Regex.IsMatch(number1.Text, "d{6,10}"))
        {
            appSettings.Add("ICE1", prefix1.Text + number1.Text);
            appSettings.Save();
        }
        else
        {
            MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
        }
        if (Regex.IsMatch(prefix2.Text, "^+\\d{2,3}") && Regex.IsMatch(number2.Text, "d{6,10}"))
        {
            appSettings.Add("ICE2", prefix2.Text + number2.Text);
            appSettings.Save();
        }
        else
        {
            MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
        }
        if (Regex.IsMatch(prefix2.Text, "^+\\d{2,3}") && Regex.IsMatch(number2.Text, "d{6,10}"))
        {
            appSettings.Add("ICE3", prefix2.Text + number2.Text);
            appSettings.Save();
        }
        else
        {
            MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
        }
        MessageBox.Show("Your information is saved...!");
        }
        else
        {
        //
            appSettings["fullName"] = fullName.Text;
            appSettings.Save();

            appSettings["address"] = address.Text;
            appSettings.Save();
            if (Regex.IsMatch(prefix1.Text, "^+\\d{2,3}") && Regex.IsMatch(number1.Text, "d{6,10}"))
            {
                appSettings["ICE1"]= prefix1.Text + number1.Text;
                appSettings.Save();
            }
            else
            {
                MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
            }
            if (Regex.IsMatch(prefix2.Text, "^+\\d{2,3}") && Regex.IsMatch(number2.Text, "d{6,10}"))
            {
                appSettings["ICE2"] = prefix2.Text + number2.Text;
                appSettings.Save();
            }
            else
            {
                MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
            }
            if (Regex.IsMatch(prefix2.Text, "^+\\d{2,3}") && Regex.IsMatch(number2.Text, "d{6,10}"))
            {
                appSettings["ICE3"] = prefix3.Text + number3.Text;
                appSettings.Save();
            }
            else
            {
                MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
            }

            MessageBox.Show("Your information is saved...!");
        }

Can someone explain how IsolatedStorageWorks? How can I correct this part because this is causing the problem:

if (String.IsNullOrEmpty((string)appSettings["ICE1"])|| 
            String.IsNullOrEmpty((string)appSettings["ICE2"]) || 
            String.IsNullOrEmpty((string)appSettings["ICE3"]) || 
            String.IsNullOrEmpty((string)appSettings["address"]) || 
            String.IsNullOrEmpty((string)appSettings["fullName"]))

Do I need to do appSettings.Save() every each time when I wish to add the data or update-it...?

I also have another problem of getting data from IsolatedStorageSettings:

     public WindowsPhoneControl2()
    {
        InitializeComponent();
    }
    string ice1;
    string ice2;
    string ice3;

    PhoneCallTask ptask = new PhoneCallTask();

    private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        ptask.PhoneNumber = ice1;
        ptask.DisplayName = "Contact person 1";
        ptask.Show();
    }

    private void Image_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
    {
        ptask.PhoneNumber = ice2;
        ptask.DisplayName = "Contact person 2";
        ptask.Show();
    }

    private void Image_Tap_2(object sender, System.Windows.Input.GestureEventArgs e)
    {
        ptask.PhoneNumber = ice3;
        ptask.DisplayName = "Contact person 3";
        ptask.Show();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
        appSettings.Contains("fullName");

        fullName.Text = (string)appSettings["fullName"];
        address.Text = (string)appSettings["address"];
        phone1.Text = (string)appSettings["ICE1"];
        phone2.Text = (string)appSettings["ICE1"];
        phone3.Text = (string)appSettings["ICE1"];

        ice1 = phone1.Text;
        ice2 = phone2.Text;
        ice3 = phone3.Text;
    }

especially this part which is supposed to get data from dictionary:

            fullName.Text = (string)appSettings["fullName"];
        address.Text = (string)appSettings["address"];
        phone1.Text = (string)appSettings["ICE1"];
        phone2.Text = (string)appSettings["ICE1"];
        phone3.Text = (string)appSettings["ICE1"];
Was it helpful?

Solution

IsolatedStorage Settins works like a dictionary.
I think you should change:

private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

I would rather use method Contains to check whether key is there:

if (userSettings.Contains("ICE1")|| 
        userSettings.Contains("ICE2") || 
        userSettings.Contains("ICE3") || 
        userSettings.Contains("address") || 
        userSettings.Contains("fullName"))

According to MSDN, Settings are saved when the Application is closed or you call Save:

Data written to the IsolatedStorageSettings object is saved when the application that
uses the class is closed. If you want your application to write to isolated storage 
immediately, you can call the Save method in application code.

EDIT
Sample code to play with:

public partial class MainPage : PhoneApplicationPage
{
  private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

  public MainPage()
  {
     InitializeComponent();

     bool isKey = userSettings.Contains("anyKey"); //toggle break point at this line
     userSettings.Add("anyKey", "myValue");
     isKey = userSettings.Contains("anyKey");
     string value = (string)userSettings["anyKey"];
  }
}

OTHER TIPS

IsolatedStorageSetting stores data in key/value pair. If there exist a key there must would be a value regarding this key. so just simpley check whether key exist in setting or not using if(settings.Contains("YourKey")). If the condition true do your work. Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top