我正在尝试计算在第1页上点击button1的次数,然后在textbox1中显示第2页的点击次数。
我尝试在下面进行编码,但它在行' String count = localSettings.Values [“Count”]上给我一个异常错误.ToString(); '。
还有其他办法可以做我想做的事吗?
Page 1
private void button1_Click(object sender, RoutedEventArgs e)
{
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
String count= localSettings.Values["Count"].ToString();
int tc = int.Parse(count);
tc++;
localSettings.Values["Count"] = tc;
}
第2页
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
textbox1.Text = localSettings.Values["Count"].ToString();
答案 0 :(得分:2)
第一次访问“计数”时,它不存在。因此localSettings.Values["Count"]
将为空。然后尝试在一个null的对象上调用ToString(),这会导致异常。
试试这个:
private void button1_Click(object sender, RoutedEventArgs e)
{
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
int tc = 0;
if( localSettings.Values["Count"] != null )
{
String count= localSettings.Values["Count"].ToString();
tc = int.Parse(count);
}
tc++;
localSettings.Values["Count"] = tc;
}