简体   繁体   English

将值从单选按钮组传递到另一个页面(Windows Phone)

[英]Passing values from radio button group to another page (Windows Phone)

I have TestPage.xaml in which i run a test with questions. 我有TestPage.xaml,在其中运行有问题的测试。 I have set maxCount=10 so when i have ten questions the test ends. 我设置了maxCount = 10,所以当我有十个问题时,测试结束。 I want to make a settingPage.xaml with 3 radio buttons 10 , 15 , 20 so when the user checks one of them to set the maxCount, it will store in IsolatedStorageSettings. 我想用三个单选按钮10,15和20来创建一个settingPage.xaml,这样当用户检查其中一个以设置maxCount时,它将存储在IsolatedStorageSettings中。 But i can't figure out how to check in my TestPage.xaml which radiobutton is clicked, to know how many questions to load? 但是我不知道如何检查我的TestPage.xaml单击哪个单选按钮,以知道要加载多少个问题?

How can I achieve this without If-Else statements? 没有If-Else语句,如何实现?

You can use a query string. 您可以使用查询字符串。 When you navigate to TestPage.xaml pass in the max count value 当您导航到TestPage.xaml ,输入最大计数值

NavigationService.Navigate(new Uri("/TestPage.xaml?maxcount=" + maxCount, UriKind.Relative));

In your TestPage.xaml page, override the OnNavigatedTo method and check the query string value passed. 在您的TestPage.xaml页面中,重写OnNavigatedTo方法并检查传递的查询字符串值。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
   string maxCount = string.Empty;
   if (NavigationContext.QueryString.TryGetValue("maxcount", out maxCount))
   {
      //parse the int value from the string or whatever you need to do
   }
}

Alternatively, you say you've stored it in isolated storage so you could also just read it back from that. 另外,您说您已将其存储在隔离的存储中,因此也可以从中读回它。 The query string method would be faster but the isolated storage method will let you read it back at a later time if the user has closed the app. 查询字符串方法会更快,但是如果用户关闭了应用程序,隔离存储方法将使您稍后再读取它。

Update based on comment 根据评论更新

You can store a file that contains the data in Isolated Storage (you should add error handling) 您可以将包含数据的文件存储在独立存储中(您应该添加错误处理)

using(var fs = IsolatedStorageFile.GetUserStoreForApplication())
using(var isf = new IsolatedStorageFileStream("maxCount.txt", FileMode.OpenOrCreate, fs))
using(var sw = new StreamWriter(isf))
{
   sw.WriteLine(maxCount.ToString());      
}

Then read it back 然后读回来

using(var fs = IsolatedStorageFile.GetUserStoreForApplication())
using(var isf = new IsolatedStorageFileStream("maxCount.txt", FileMode.Open, fs))
using(var sr = new StreamReader(isf)
{
   string maxCount = sr.ReadToEnd();
   //you now have the maxCount value as string
   //... 
}

See draw back here with isolated storage is it will take up a memory space even when you are not running the app. 请参阅此处的隔离存储回退,因为即使您不运行应用程序,它也会占用内存空间。 So while the app is running why don't you go ahead and save your options in Application.Current.Resources for example : 因此,当应用程序运行时,为什么不继续将选项保存在Application.Current.Resources ,例如:

Application.Current.Resources.Add("Maybe Question section", 50); //will load 50 questions for particular section.

and while fetching 而在获取时

Application.Current.Resources["Maybe Question section"]

and then tryParse it to integer and get the number. 然后尝试将其tryParse为整数并获取数字。 this will be application wide until the app is running . 在应用程序运行之前,它将是整个应用程序范围的。 You can fetch every time for a particular section. 您可以每次获取特定部分的内容。 No need to connect to the isolated storage again and again to fetch or keep modifying files. 无需一次又一次连接到隔离存储中来获取或保持修改文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM