1

我有 TestPage.xaml,我在其中运行问题测试。我设置了 maxCount=10,所以当我有十个问题时,测试结束。我想用 3 个单选按钮 10 、 15 、 20 制作一个 settingPage.xaml,所以当用户检查其中一个按钮来设置 maxCount 时,它将存储在 IsolatedStorageSettings 中。但我不知道如何在我的 TestPage.xaml 中检查单击了哪个单选按钮,以知道要加载多少问题?

如果没有 If-Else 语句,我如何实现这一点?

4

2 回答 2

0

请参阅此处使用隔离存储的缺点,即使您不运行应用程序,它也会占用内存空间。因此,当应用程序运行时,您为什么不继续保存您的选项,Application.Current.Resources 例如:

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

并在获取时

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

然后将tryParse其转换为整数并获取数字。在应用程序运行之前,这将是应用程序范围内的。您可以每次获取特定部分的信息。无需一次又一次地连接到隔离存储来获取或不断修改文件。

于 2013-09-13T06:03:59.510 回答
0

您可以使用查询字符串。当您导航以TestPage.xaml传递最大计数值时

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

在您的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
   }
}

或者,您说您已将其存储在隔离存储中,因此您也可以从中读取它。查询字符串方法会更快,但如果用户关闭了应用程序,隔离存储方法可以让您稍后再读回它。

根据评论更新

您可以将包含数据的文件存储在独立存储中(您应该添加错误处理)

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());      
}

然后读回来

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
   //... 
}
于 2013-09-12T22:34:35.650 回答