在C#中的页面之间传递数据

时间:2014-03-18 08:43:24

标签: c# generics windows-phone

您好我正在尝试将数据从mt Generic list of pets传递给" Profile"页面和我使用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));

    }

最后这是我应该(我相信)将数据加载到我在APP页面上创建的文本块中的位置(见下图)

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;
        }   
    }   

所以我的问题是,如果你们中的任何人能够告诉我为什么它不会将所有数据传递到新页面并且可能的修复,那将非常感激。此外,如果您需要查看我的代码的任何其他部分,只需评论告诉我您需要什么,然后我将添加它。

提前致谢,杰森

////照片管理\\ 这是我将选择动物>>> http://gyazo.com/4e39a228aca8ceff1cb83f54b7ba9d89

这表明它只传递了第一条数据(名称)>> http://gyazo.com/832f262b46191e5e0992f02533a6b632

4 个答案:

答案 0 :(得分:0)

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

导航:

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...
    }

你应该避免网址中的长数据

您可能会遇到问题(NavigationContext.QueryString中没有数据),因为您没有转义值。试试Uri.Escapedatastring

    NavigationService.Navigate(new Uri("/Profile.xaml?" + "name=" + Uri.EscapeDataString(selectedPet.Name) + " & " + "age=" + Uri.EscapeDataString(selectedPet.Age) + " & " + "breed=" + Uri.EscapeDataString(selectedPet.Breed) + " & " + "price=" + Uri.EscapeDataString(selectedPet.Price), UriKind.RelativeOrAbsolute));

答案 1 :(得分:0)

Querystrings不得用于传递完整项目,因为查询字符串只传递单个变量。

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

保存您的对象就在对selectionchenged事件进行导航之前

 IsolatedStorageSettings.ApplicationSettings["State"] = selectedPet;
 IsolatedStorageSettings.ApplicationSettings.Save();

并在Profile.xaml页面上OnNavigatedTo Handler只需返回对象

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

答案 2 :(得分:0)

正如阿曼所指出的那样,你正在尝试做的最好方法是使用IsolatedStorage。但我不同意他的建议。 IsolatedStorageSettings适用于应用设置。使用它来传递对象是一种破解。

我的建议是,serialize将您的对象IsolatedStorage传递给deserialize,然后传递文件名,以便{另一方面} EZ_Iso。{/ p>

有一个名为serialize的DLL可让您deserializeEZ_Iso.IsolatedStorageAccess.SaveFile("SelectedPet",YourSelectedPetObject); 进行单个方法调用。超级高效。

Get EZ_Iso.dll and Documentation Here

您案例的例子是

序列化

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

然后反序列化

{{1}}

执行此操作将清除代码的TON。你不需要在app.cs文件中有令人作呕的全局变量。采用这样的体系结构更符合MVVM,并且更容易对序列化产生的结果进行数据绑定。

答案 3 :(得分:-1)

您可以通过四种方式在页面之间传递数据,这在后面的帖子

中有明确说明

http://nishantcop.blogspot.in/2011/08/passing-data-between-pages-in-windows.html