silverlight, ListBox navigation to new page with o

2019-07-30 13:19发布

问题:

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

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

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)

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.

回答1:

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

  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.

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

Code to put in App.cs: MyObject selectedObject;

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

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

  1. You can set the Object in the DataContext of your LayoutRoot, but i don't have the code for that on top of my head.


回答2:

The answer here is simple - you cannot directly pass an object to another page. You can serialize it to JSON or XML and then deserialize it on the target page, but the serialized item will still have to be passed as a parameter.



回答3:

Instead of sending the selectedindex as a query string parameter you could send the ID for the object or similar, something that uniquely can identify the object.

Then in the details page you could fetch the correct object from the same datasource that the main list box get its data from (in your case "itemList" which could come from e.g. IsolatedStorage).

If itemList is instantiated and kept only within the main page then you won't be able to fetch the item by ID from the details page. So in that case you'd need to move the itemList to some static or app level storage.

HTH