silverlight,ListBox 导航到带有对象的新页面可能吗? [英] silverlight, ListBox navigation to new page with object possible?

查看:22
本文介绍了silverlight,ListBox 导航到带有对象的新页面可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个列表框 MainListBox,我可以在其中动态添加项目.现在,当我在列表框中选择一个项目时,我想导航到 DeticalsPage.xaml.cs.然后我可以在其中显示有关所选项目的信息.

Hi all i have a listbox MainListBox where i add items to dynamically. Now i want to navigate to DetialsPage.xaml.cs when i choose an item in the listbox. where i can then display my info about the selected item.

private void SetListBox()
{
    foreach (ToDoItem todo in itemList)
    {
        MainListBox.Items.Add(todo.ToDoName);
    }
}

MainListBox_SelectionChanged(由visual studio 2010 silverlight 为windows 7 手机生成)

MainListBox_SelectionChanged ("Generated by visual studio 2010 silverlight for windows 7 phone)

// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // If selected index is -1 (no selection) do nothing
    if (MainListBox.SelectedIndex == -1)
        return;

    // Navigate to the new page
    NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

    // Reset selected index to -1 (no selection)
    MainListBox.SelectedIndex = -1;
}

在 DetailsPage.xaml.cs 中是下一个方法.(由visual studio 2010 silverlight for windows 7 phone 生成)我知道下面的方法不符合我的尝试.

in DetailsPage.xaml.cs is the next method. ("Generated by visual studio 2010 silverlight for windows 7 phone) I'm aware that the below method does not do what i try.

// When page is navigated to set data context to selected item in list
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string selectedIndex = "";
    if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
    {
        int index = int.Parse(selectedIndex);
        DataContext = App.ViewModel.Items[index];
    }
}

我想访问 selectedIndex 并调用 MainListbox 中对象的方法所以基本上:Mainlistbox => 选择项目 => 将该项目发送到详细信息页面 => 详细信息页面访问该项目并调用该项目(对象)上的方法

I would like to access the selectedIndex and call my methods of my object that is in the MainListbox so Basicly: Mainlistbox => select item => send that item to details page => details page access the item and call methods on the item (object)

我确定这是一个基本问题,很难找到任何细节.我想补充一点,这是我的第一个 Windows Phone 7 应用.

I'm sure this is a basic question tough it seems hard to find any specifics on it. i would like to add that this is my first windows phone 7 app.

推荐答案

可以通过多种方式在页面之间传递对象:

There are many ways you can pass an object from page to page:

  1. 像 Dennis 所说的那样进行序列化和反序列化,但这虽然可行,但并不实用,除非您想将对象保存在独立存储中并稍后检索.

  1. serialize and deserialize like Dennis said, but this, although feasable, is not practical, unless you want to save the object in isolated storage and retrieve it later.

在 App.cs 类中放置一个对象,该类可供所有页面访问.在母版页中设置您的对象,从详细信息页检索它.

Place an object in the App.cs class, which is accessible to all pages. Set your object in the master page, retrieve it from the Details page.

放入App.cs的代码:MyObject selectedObject;

Code to put in App.cs: MyObject selectedObject;

放入 MasterPage.cs 的代码:application.selectedObject = MainListBox.selectedItem;

Code to put in MasterPage.cs: application.selectedObject = MainListBox.selectedItem;

放入DetailsPage.cs的代码:MyObject selectedObject = application.seletedObject;

Code to put in DetailsPage.cs: MyObject selectedObject = application.seletedObject;

  1. 您可以在 LayoutRoot 的 DataContext 中设置对象,但我没有相关代码.

这篇关于silverlight,ListBox 导航到带有对象的新页面可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆