【问题标题】:IsolatedStorageSettings WP8隔离存储设置 WP8
【发布时间】:2015-05-07 16:39:59
【问题描述】:

我有一个无法解决的问题(至少我还没有找到解决方案...)我想将数据添加到 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"];

【问题讨论】:

  • 你得到什么异常?

标签: windows-phone-8 isolatedstorage


【解决方案1】:

IsolatedStorage Settins 的工作方式类似于 dictionary。
我认为你应该改变:

private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

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

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

【讨论】:

  • 但是如果这个字段没有启动,则不存在。这意味着 userSettings 将具有空值。根据 MSDN msdn.microsoft.com/en-us/library/cc614991%28v=vs.95%29.aspx 它会给我一个空键值的例外。然后呢?
  • 在测试字典中的特定字段之前使用 userSettings.Count > 0 是否明智?
  • ApplicationSettings 属于您的应用程序:msdn.microsoft.com/en-us/library/windowsphone/develop/… 并且您使用 ApplicationSettings 属性 msdn.microsoft.com/en-us/library/… 来启动该字段(我上面的第一行代码 private ...)方法包含(您要求)将返回您的布尔值取决于键是否存在,或者如果您输入错误的名称或键,则抛出异常。如果您知道键的确切名称,您可以检查它是否存在于字典中。
  • 我还添加了一个示例代码 - 在调试模式下运行,在 bool isKey 处设置断点......然后向前看,看看用户设置和其他变量是如何变化的。
  • 感谢 Romasz,我将修改我的代码并尝试您的建议。我下班回家后会测试它。欢呼
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    相关资源
    最近更新 更多