C# 用C语言在页面之间传递数据

C# 用C语言在页面之间传递数据,c#,generics,windows-phone,C#,Generics,Windows Phone,您好,我目前正在尝试将数据从mt通用宠物列表传递到配置文件页面,我正在使用OnNavigatedTo方法来完成此操作,唯一的问题是它只接受我希望传递的第一项,而忽略了其余的项 这就是我在App.XAML中创建通用列表的地方 private void Application_Launching(object sender, LaunchingEventArgs e) { myshop.Add(new Shop{Name = "Johnny", Age= 2, Bre

您好,我目前正在尝试将数据从mt通用宠物列表传递到配置文件页面,我正在使用OnNavigatedTo方法来完成此操作,唯一的问题是它只接受我希望传递的第一项,而忽略了其余的项

这就是我在App.XAML中创建通用列表的地方

private void Application_Launching(object sender, LaunchingEventArgs e)
    {
            myshop.Add(new Shop{Name = "Johnny", Age= 2, Breed="Husky", Type= "Dog", Stock = 1, Price = 125, Photo = "/Assignment 1;component/Images/Husky.jpg"});
            myshop.Add(new Shop{Name = "Billy", Age= 1, Breed="Shiba Inu", Type= "Dog", Stock = 1, Price = 250, Photo = "/Assignment 1;component/Images/Shiba Inu.jpg"});
            myshop.Add(new Shop{Name = "Sammy", Age = 8, Breed="Siamese", Type="Cat", Stock = 1, Price = 15, Photo = "/Assignment 1;component/Images/Siamese Cat.jpg"});
            myshop.Add(new Shop{Name = "Molly", Age = 6, Breed="Norwegian", Type="Cat", Stock = 1, Price = 30, Photo = "/Assignment 1;component/Images/NorwegianForestCat.jpeg"});
            myshop.Add(new Shop{Name = "Nemo", Age = 3, Breed="Clown Fish", Type="Fish", Stock = 1, Price = 10, Photo = "/Assignment 1;component/Images/clown Fish.jpg"});
            myshop.Add(new Shop{Name = "Dory", Age = 1, Breed="Palette SurgeonFish", Type="Fish", Price = 75, Stock = 1, Photo = "/Assignment 1;component/Images/Palette Surgeonfish.jpg"});
            myshop.Add(new Shop{Name = "Keith", Age = 4, Breed="Bearded Dragon", Type="Lizard", Stock = 1, Price = 750, Photo = "/Assignment 1;component/Images/Bearded Dragon.jpg"});
            myshop.Add(new Shop {Name = "Oisin", Age = 12, Breed = "Gecko", Type = "Lizard", Stock = 1, Price = 90, Photo = "/Assignment 1;component/Images/Gecko.jpg" });
    }
然后,我已经开始在我的通用列表中选择一个项目时传递数据

private void list_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        Shop selectedPet = list.SelectedItem as Shop;
        NavigationService.Navigate(new Uri("/Profile.xaml?" + "name=" + selectedPet.Name + " & " + "age=" + selectedPet.Age + " & " + "breed=" + selectedPet.Breed + " & " + "price=" + selectedPet.Price, UriKind.RelativeOrAbsolute));

    }
最后,我认为应该在这里将数据加载到我在应用程序页面上创建的文本块中,见下图

public partial class Profile : PhoneApplicationPage
{
    App thisApp = Application.Current as App;

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        string name;
        string age;
        string breed;
        string price;

        if (NavigationContext.QueryString.TryGetValue("name", out name))
        {
            nameTxtBlock.Text = name;
        }
        else if(NavigationContext.QueryString.TryGetValue("age", out age))
        {
            ageTxtBlock.Text = age;
        }
        else if (NavigationContext.QueryString.TryGetValue("breed", out breed))
        {
            breedTxtBlock.Text = breed;
        }
        else if(NavigationContext.QueryString.TryGetValue("price", out price))
        {
            priceTxtBlock.Text = price;
        }   
    }   
所以我的问题是,如果你们中的任何人能告诉我为什么它不能将所有的数据传递到新页面,以及一个可能的修复方法,我将不胜感激。此外,如果您需要查看我的代码的任何其他部分,只需注释,告诉我您需要什么,然后我将添加它

提前谢谢你,杰森

////照片\\ 这是我选择动物的地方>>>


这表明它只传递第一段数据名>>

您只能将标识符传递给页面。假设名称是唯一的,并且是对象的标识符

要导航:

NavigationService.Navigate(new Uri("/Profile.xaml?" + "name=" + selectedPet.Name, UriKind.RelativeOrAbsolute));
然后在配置文件页面中:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    string name;
    Shop currentShop;

    if (NavigationContext.QueryString.TryGetValue("name", out name))
    {
        //Use your repository to fetch the object
        currentShop = App.myshop.First(s => s.Name == name);
        //Then initialize with fetched object...
    }
您应该避免URL中的长数据

由于未转义值,可能会出现NavigationContext.QueryString中不存在数据的问题。尝试:


查询字符串不能用于传递完整项,因为查询字符串只传递一个变量

可以在IsolatedStorage应用程序设置中存储用户定义的对象

在执行“选择时导航”事件之前保存对象

 IsolatedStorageSettings.ApplicationSettings["State"] = selectedPet;
 IsolatedStorageSettings.ApplicationSettings.Save();
在Profile.xaml页面上导航到处理程序,只需将对象取回即可

 if (IsolatedStorageSettings.ApplicationSettings.Contains("State") == true)
 {
 var object= IsolatedStorageSettings.ApplicationSettings["State"] as Shop;
 //Remove the state now
 IsolatedStorageSettings.ApplicationSettings.Remove("State");
 IsolatedStorageSettings.ApplicationSettings.Save();
 }

您可以通过四种方式在页面之间传递数据,这在下面的文章中有明确的解释


正如Aman所指出的,做您正试图做的事情的最好方法是使用隔离存储。然而,我不同意他的建议。IsolatedStorage设置用于应用程序设置。使用它传递对象是一种黑客行为

我的建议是将对象序列化为IsolatedStorage,然后传递文件名,以便在另一端对其进行反序列化

有一个名为EZ_Iso的DLL,允许您通过单个方法调用进行序列化和反序列化。超级高效

你的例子是

将其序列化

EZ_Iso.IsolatedStorageAccess.SaveFile("SelectedPet",YourSelectedPetObject);
   Shop yourPet = (Shop)EZ_Iso.IsolatedStorageAccess.GetFile("SelectedPet",typeof(Shop)); 
然后将其反序列化

EZ_Iso.IsolatedStorageAccess.SaveFile("SelectedPet",YourSelectedPetObject);
   Shop yourPet = (Shop)EZ_Iso.IsolatedStorageAccess.GetFile("SelectedPet",typeof(Shop)); 

这样做将清理大量代码。你不需要在app.cs文件中有恶心的globals。采用这样的体系结构更符合MVVM,也更容易对序列化结果进行数据绑定

指向图片的链接已断开。请将它们上载到StackOverflow或其他可靠的映像主机。这是一个错误,Uri.EscapeDataStringselectedPet.Name的工作原理与中相同,它没有给出错误,但另外两个Uri.EscapeDataString的带下划线的错误表示“System.Uri.EscapeDataStringstring”的最佳重载方法匹配有一些无效参数。@JasonL95 Add.ToString当需要时:selectedPet.Age.ToStringIt现在在单击精选宠物。我使用了一个断点,这就是错误的来源,有什么帮助吗?受保护的覆盖无效OnNavigatedToSystem.Windows.Navigation.NavigationEventTargets e{string name;Shop currentShop;if NavigationContext.QueryString.TryGetValuename,out name{//使用存储库获取对象currentShop=thisApp.myshop.Firsts=>s.Name==Name;//然后使用获取的对象初始化…}