简体   繁体   English

Windows Phone 7-从列表框中传递/传输所选数据

[英]Windows Phone 7 - Passing / transferring selected data from listbox

I am creating an app where I have obtained data from a DB Via Web service and displaying it within a listbox within my mainpage.xaml (data about different events being held eg EventTitle, Date etc.) At the moment I have set it up so that when an item within the list box is selected the application navigates to an "EventDetail" page which portrays only the selected data from the list. 我正在创建一个应用程序,在该应用程序中,我已通过Web服务从数据库获取数据,并将其显示在mainpage.xaml的列表框中(有关举行的不同事件的数据,例如EventTitle,Date等)。当选择列表框中的项目时,应用程序导航到“ EventDetail”页面,该页面仅描绘列表中的选定数据。

What I need help with is taking this user selected data and taking it to the navigated page to be used. 我需要帮助的是将此用户选择的数据带入要使用的导航页面。 I am struggling to figure out in the simplest way; 我正在努力以最简单的方式解决问题。 how to transfer a listbox item which has been selected by the user to the defined page to display it in a more clear way and to use this data. 如何将用户已选择的列表框项目传输到定义的页面,以更清晰的方式显示它并使用此数据。

Preferably I would like to display each textblock/cell (EventTitle, Date etc) in separate text blocks within the navigated page so that they can be laid out appropriately but a list box with only the selected fields data would do the job. 最好是,我想在导航页面内的单独文本框中显示每个文本块/单元格(EventTitle,Date等),以便可以适当地布局它们,但是仅包含选定字段数据的列表框可以完成此工作。

Here is the relevant Xaml code: 这是相关的Xaml代码:

   <ListBox  Height="496" HorizontalAlignment="Left" Margin="-4,155,0,0" Name="FirstListBox2" VerticalAlignment="Top" Width="460" SelectionChanged="FirstListBox2_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="132">

                            <StackPanel Width="370">
                                <TextBlock Text="{Binding EventID}" Foreground="#FFC8AB14" FontSize="24" />
                                <TextBlock Text="{Binding EventList}" TextWrapping="Wrap" FontSize="36" />
                                <TextBlock Text="{Binding Date}" TextWrapping="Wrap" FontSize="24" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Here is the relevant C# coding: 以下是相关的C#编码:

     private void FirstListBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        EventServiceReference1.Event myEvent = (EventServiceReference1.Event)FirstListBox2.SelectedItem;
        NavigationService.Navigate(new Uri("/EventPageTemp.xaml", UriKind.Relative));
    }

Please if you could help me with this problem I would very much appreciate. 如果您能帮助我解决这个问题,请多多关照。

Use a query-string parameter to send the selected value: 使用查询字符串参数发送所选值:

EventServiceReference1.Event myEvent = (EventServiceReference1.Event)FirstListBox2.SelectedItem;
int eventId = myEvent.EventID;
string url = string.Format("/EventPageTemp.xaml?eventId={0}", eventId);
NavigationService.Navigate(new Uri(url, UriKind.Relative));

You can pick up the event parameter on the target page using NavigationContext.QueryString . 您可以使用NavigationContext.QueryString 在目标页面上拾取事件参数 Then just set the data context of your target page as needed: 然后只需根据需要设置目标页面的数据上下文:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    int eventId;
    string eventIdStr;
    if (NavigationContext.QueryString.TryGetValue("eventId", out eventIdStr) && int.TryParse(eventIdStr, out eventId))
    {
        // load event data, and set data context
    }
}

Edit 编辑

I guess you might want to save the event data temporarily, rather than re-loading it from the service on the target page. 我猜您可能想临时保存事件数据,而不是从目标页面上的服务重新加载它。 If so, you could use isolated storage to save the event data. 如果是这样,您可以使用隔离存储来保存事件数据。 So before navigating, add the data using an appropriate key: 因此,在导航之前,请使用适当的键添加数据:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["event"] = myEvent;
settings["eventId"] = eventId;

And then pick it up in the same way: 然后以相同的方式将其拾取:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.ContainsKey("event") && (int)settings["eventId"] == eventId)
{
    var myEvent = (EventServiceReference1.Event)settings["event"];
}

Edit #2 编辑#2

As AndreiC points out, writing to isolated storage takes up space on the device, so you should remove any items that are no longer needed. 正如AndreiC指出的那样,写入隔离存储会占用设备空间,因此您应该删除不再需要的所有项目。

A simple approach would be to add a public static property of type Event to your App class (name it SelectedEvent) so when the user selects an event from the ListBox you would set the: 一种简单的方法是将Event类型的公共静态属性添加到您的App类(将其命名为SelectedEvent),因此,当用户从ListBox中选择一个事件时,您可以设置:

App.SelectedEvent = myevent;

Then in the EventPageTemp.xaml.cs you would just set the DataContext of the page to your App.SelectedEvent: 然后在EventPageTemp.xaml.cs中,只需将页面的DataContext设置为App.SelectedEvent:

DataContext = App.SelectedEvent;

In the xaml page of the EventPageTemp you would just bind the interface to the properties of your Event object. 在EventPageTemp的xaml页面中,您只需将接口绑定到Event对象的属性即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM