【发布时间】: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