我正在创建一个应用程序,在该应用程序中,我已通过Web服务从数据库获取数据,并将其显示在mainpage.xaml的列表框中(有关所举行的不同事件的数据,例如EventTitle,Date等)。当列表框中选定某个项应用程序将导航到“EventDetail”页面,从列表中只描绘所选择的数据。
我需要帮助的是将此用户选择的数据带入要使用的导航页面。我正在努力以最简单的方式找出答案;如何将用户已选择的列表框项目传输到定义的页面,以便以更清晰的方式显示它并使用此数据。
最好是,我想在导航页面内的单独文本框中显示每个文本块/单元格(EventTitle,Date等),以便可以适当地布局它们,但是仅包含选定字段数据的列表框可以完成工作。
这是相关的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>
以下是相关的C#编码:
private void FirstListBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
EventServiceReference1.Event myEvent = (EventServiceReference1.Event)FirstListBox2.SelectedItem;
NavigationService.Navigate(new Uri("/EventPageTemp.xaml", UriKind.Relative));
}
如果您能帮助我解决这个问题,请多多关照。
使用查询字符串参数发送所选值:
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));
您可以使用在目标页面上拾取事件参数NavigationContext.QueryString
。然后只需根据需要设置目标页面的数据上下文:
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
}
}
编辑
我猜您可能想临时保存事件数据,而不是从目标页面上的服务重新加载它。如果是这样,您可以使用隔离存储来保存事件数据。因此,在导航之前,请使用适当的键添加数据:
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["event"] = myEvent;
settings["eventId"] = eventId;
然后以相同的方式将其拾取:
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.ContainsKey("event") && (int)settings["eventId"] == eventId)
{
var myEvent = (EventServiceReference1.Event)settings["event"];
}
编辑#2
正如AndreiC所指出的,写入隔离存储会占用设备空间,因此您应该删除不再需要的所有项目。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句