IsolatedStorage and navigation

本文关键字:navigation and IsolatedStorage | 更新日期: 2023-09-27 18:01:16

我无法解决这个奇怪的问题,我已经尝试了所有我能想到的方法。

我有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{系统。ArgumentException:值不在预期范围内。}系统。异常{系统。ArgumentException}

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

IsolatedStorage and navigation

所以在页面之间传递数据的方法有很多。

你选择的方式是最不可取的。

你可以使用PhoneApplicationService。当前的字典,但如果你有大量的变量,这也会很混乱,在应用程序关闭后不会持续存在,可以简化。

我写了一个免费的DLL,它把这个确切的场景记在心里,叫做EZ_iso。

你可以在这里找到

基本上你会这样使用它。

[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