【问题标题】:IsolatedStorage and navigation隔离存储和导航
【发布时间】:2014-05-02 15:59:57
【问题描述】:

我无法解决这个奇怪的问题,我已经尝试了任何我能想到的东西。

我有 5 个页面,每个页面都以这种方式通过导航传递变量:

通过:

NavigationSerice.Navigate(new Uri("/myPage.xaml?key=" + myVariable, UriKind.Relative));

检索:

If (NavigationContext.QueryString.ContainsKey(myKey))
{
    String retrievedVariable = NavigationContext.QueryString["myKey"].toString();
}

我在许多页面上打开一个列表,其中一个页面自动从列表中删除一个项目 actualProject(actualProject 是字符串列表的变量)。然后,当我回溯到某个特定页面时,该应用程序会引发异常。为什么?我不知道。

删除项目的代码:

                // Remove the active subject from the availible subjects
            unlinkedSubjects.Remove(actualSubject);
            unlinkedsubjectsListBox.ItemsSource = null;
            unlinkedsubjectsListBox.ItemsSource = unlinkedSubjects;

然后是抛出异常的OnNavigatedTo事件的页面:

        protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (NavigationContext.QueryString.ContainsKey("key"))
        {
            actualProject = NavigationContext.QueryString["key"];
            try
            {
                //Read subjectList from IsolatedStorage
                subjectList = readSetting(actualProject) != null ? (List<String>)readSetting(actualProject) : new List<String>();

                //Put the subjectList into the subjectListBox
                subjectListBox.ItemsSource = subjectList;

                //Set the subjectsPageTitle to the "actualProject" value, to display the name of the current open project at the top of the screen
                subjectsPageTitle.Text = actualProject;
            }
            catch (Exception)
            {
                if (language.Equals("en."))
                {
                    // Language is set to english
                    MessageBox.Show("Couldn't open the project, please try again or please report the error to Accelerated Code - details on the about page");
                }
                else if (language.Equals("no."))
                {
                    // Language is set to norwegian
                    MessageBox.Show("Kunne ikke åpne prosjektet, vennligst prøv igjen eller rapporter problemet til Accelerated Code - du finner detaljer på om-siden");
                }

            }
        }
    }

例外:

  • _exception {System.ArgumentException:值不在预期范围内。} System.Exception {System.ArgumentException}

我的理论: 应用程序类型加载当前打开和修改的列表。那可能吗?不知道。

【问题讨论】:

  • 你能写出它崩溃在哪一行吗?
  • 对不起,我没有看到你的消息。明天写:)

标签: c# windows-phone-8 navigation windows-phone isolatedstorage


【解决方案1】:

因此,有多种方法可以在页面之间传递数据。

您选择的方式是最少建议的。

You can use the PhoneApplicationService.Current dictionary 但是,如果您有大量变量,在应用程序关闭后不会持续存在并且可以简化,这也很麻烦。

我写了一个免费的 DLL 来记住这个确切的场景,叫做 EZ_iso。

You can find it here

基本上你会用它做什么。

[DataContractAttribute]
public class YourPageVars{
   [DataMember]
   public Boolean Value1 = false;

   [DataMember]
   public String Value2 = "And so on";

   [DataMember]
   public List<String> MultipleValues;
}

一旦你完成了你的课程设置,你就可以在页面之间轻松地传递它

YourPageVars vars = new YourPageVars { /*Set all your values*/ };

//Now we save it

EZ_iso.IsolatedStorageAccess.SaveFile("PageVars",vars);

就是这样!现在您可以导航和检索文件了。

YourPageVars vars = (YourPageVars)EZ_iso.IsolatedStorageAccess.GetFile("PageVars",typeof(YorPageVars));

这很好,因为您可以将其用于导航以外的其他用途。您可以将它用于需要隔离存储的任何事物。此数据现在已序列化到设备,因此即使应用程序关闭,它也会保留。如果您愿意,当然也可以随时删除该文件。

请务必参阅文档以了解您遇到的任何例外情况。如果您仍然需要帮助,请随时在推特上联系我@Anth0nyRussell 或 amr@AnthonyRussell.info

【讨论】:

  • 很好的答案 :) - 明天肯定会尝试,这里有点晚了 - 明天工作 :)
  • 不是自助服务,但它确实是一个很棒的 DLL。我创建的应用程序几乎没有我不使用它。就像我说的,如果您有任何问题,请随时联系我。
  • 我现在正在调查我的问题,非常好的答案 - 但您认为这是导致我的应用崩溃的问题吗?每当我删除此代码时,它都会起作用: unlinkedSubjects.Remove(actualSubject);
  • 如果您发布的代码是您实际源代码中的代码,那么我认为它没有理由抛出该错误。因此很难诊断。这就是为什么我发布了我所做的答案。这个 DLL 比您使用的方法更具可扩展性。您可以像使用此 DLL 传递 1 个变量一样轻松地传递 1000 个变量,而带有导航变量的 1000 个变量将是一场噩梦
  • 这就是它如此困难的原因 - 我必须确定,因为将我的应用程序中的所有导航代码切换到新的 dll 需要很长时间:) 很棒的代码,但是切换并导致同样的错误太糟糕了
猜你喜欢
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多