【问题标题】:Passing values from radio button group to another page (Windows Phone)将值从单选按钮组传递到另一个页面 (Windows Phone)
【发布时间】:2013-09-13 14:03:59
【问题描述】:

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

如果没有 If-Else 语句,我该如何实现?

【问题讨论】:

  • 发布您的代码(XAML 和代码隐藏)

标签: c# windows-phone-7 windows-phone-8 radio-button


【解决方案1】:

您可以使用查询字符串。当您导航到 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
   //... 
}

【讨论】:

  • 就像你提到的,当用户稍后重新打开应用程序时,我想使用隔离存储来保持当前状态。我需要从我的设置页面单击的单选按钮加载设置。
  • 我已经通过使用隔离存储的示例更新了答案。我不在我的开发机器上,所以我无法测试它是否可以编译,但这应该让您了解如何使用隔离存储。
【解决方案2】:

请参阅此处使用隔离存储的缺点,即使您不运行应用程序,它也会占用内存空间。因此,当应用程序运行时,为什么不继续将您的选项保存在 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 转换为整数并获取数字。在应用程序运行之前,这将是应用程序范围内的。您可以每次获取特定部分的信息。无需一次又一次地连接到隔离存储来获取或不断修改文件。

【讨论】:

  • 不错的方法,但是如果我想在每次用户重新打开应用程序时保持当前设置更改怎么办?
  • 如果应用程序重新打开时需要持久化,请使用 linq to sql 存储在数据库中。这是链接 msdn.microsoft.com/en-us/library/windowsphone/develop/hh202860(v=vs.105).aspx 。仅使用图像或 pdf 或其他文件来存储到隔离存储。对于问题答案和其他数据字符串,请使用 linq。
猜你喜欢
  • 2020-03-02
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2016-12-19
  • 1970-01-01
相关资源
最近更新 更多