IsolatedStorageSettings WP8

时间:2013-11-19 20:09:07

标签: windows-phone-8 isolatedstorage

我有一个我无法解决的问题(至少我还没有找到解决方案......)我想在WP8中将数据添加到IsolatedStorageSettings中。我的目标是检查那里是否有设置,但是当我想检查时,我得到一个我无法解决的异常。

    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...!");
        }

有人能解释一下IsolatedStorageWorks如何? 如何更正此部分,因为这会导致问题:

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.Save()?

我还有另一个从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;
    }

特别是这部分应该从字典中获取数据:

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

2 个答案:

答案 0 :(得分:4)

IsolatedStorage Settins就像dictionary一样 我想你应该改变:

private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

我宁愿使用方法Contains检查密钥是否存在:

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

根据MSDN,当应用程序关闭或您调用保存时保存设置:

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.

修改
要使用的示例代码:

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

答案 1 :(得分:0)

IsolatedStorageSetting将数据存储在键/值对中。如果存在密钥,则必须存在关于该密钥的值。所以,只需使用if(settings.Contains("YourKey"))检查设置中是否存在密钥。如果条件为真,你的工作。感谢