[英]Count number of clicks and display on another page [Windows Phone c#]
I am trying to count how many times button1 is clicked on page1, then display the amount of clicks on page2 in a textbox1. 我试图计算在page1上单击button1的次数,然后在textbox1中显示在page2上的点击量。
I've tried to code below but it is giving me an exception error on the line ' String count= localSettings.Values["Count"].ToString(); 我尝试在下面进行编码,但是这在行' String count = localSettings.Values [“ Count”]。ToString();中给了我一个异常错误。 '. ”。
Is there another way to do what I want to? 还有另一种方法可以做我想做的事吗?
Page 1 第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;
}
Page 2 第2页
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
textbox1.Text = localSettings.Values["Count"].ToString();
When you access "Count" for the first time, it does not exist. 首次访问“计数”时,它不存在。 localSettings.Values["Count"]
is therefore going to be null. localSettings.Values["Count"]
将为null。 You then try to call ToString() on an object that is null and that causes the exception. 然后,您尝试在null并导致异常的对象上调用ToString()。
Try this: 尝试这个:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.