首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IsolatedStorage与导航

IsolatedStorage与导航
EN

Stack Overflow用户
提问于 2014-05-02 15:59:57
回答 1查看 76关注 0票数 2

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

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

通行证:

代码语言:javascript
复制
NavigationSerice.Navigate(new Uri("/myPage.xaml?key=" + myVariable, UriKind.Relative));

检索:

代码语言:javascript
复制
If (NavigationContext.QueryString.ContainsKey(myKey))
{
    String retrievedVariable = NavigationContext.QueryString["myKey"].toString();
}

我在多个页面上打开一个列表,其中一个页面会自动从list actualProject中删除一个项(actualProject是字符串列表的一个变量)。然后,当我回到如此遥远的地方,我到达一个特定的页面-应用程序抛出一个异常。为什么?我没有头绪。

删除该项的代码:

代码语言:javascript
复制
                // Remove the active subject from the availible subjects
            unlinkedSubjects.Remove(actualSubject);
            unlinkedsubjectsListBox.ItemsSource = null;
            unlinkedsubjectsListBox.ItemsSource = unlinkedSubjects;

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

代码语言:javascript
复制
        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}

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

EN

回答 1

Stack Overflow用户

发布于 2014-05-02 18:31:46

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

你选择的方式是最不明智的。

您可以使用PhoneApplicationService.Current字典 --但是如果你有大量的变量,在应用程序关闭后不会一直存在,并且可以简化,这也是很麻烦的。

我编写了一个自由的DLL,它将这个精确的场景记在脑海中,称为EZ_iso。

你都能在这找到你的需要

从根本上说,你要做的就是使用它。

代码语言:javascript
复制
[DataContractAttribute]
public class YourPageVars{
   [DataMember]
   public Boolean Value1 = false;

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

   [DataMember]
   public List<String> MultipleValues;
}

一旦您有了类设置,您就可以轻松地在页面之间传递它。

代码语言:javascript
复制
YourPageVars vars = new YourPageVars { /*Set all your values*/ };

//Now we save it

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

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

代码语言:javascript
复制
YourPageVars vars = (YourPageVars)EZ_iso.IsolatedStorageAccess.GetFile("PageVars",typeof(YorPageVars));

这很好,因为您可以使用它进行更多的导航。您可以将它用于任何需要隔离存储的地方。这些数据现在被序列化到设备上,所以即使应用程序关闭,它也将保持不变。当然,如果您选择的话,也可以随时删除该文件。

如果有任何异常,请务必参考文档。如果你还需要帮助,可以在twitter、@Anth0nyRussell或amr@AnthonyRussell.info上给我打电话。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23432303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档