Windows Phone 7导航传递参数

时间:2012-01-23 12:39:49

标签: c# visual-studio-2010 windows-phone-7

我有一个如下所示的列表框。

<ListBox x:Name="CouponListBox" ItemsSource="{Binding Item}" SelectionChanged="CouponListBox_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="{Binding MerchantImage}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
                                <StackPanel Width="130">
                                    <TextBlock Text="{Binding CustomerName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                    <TextBlock Text="{Binding MerchantName}" TextWrapping="Wrap" FontSize="20" Foreground="#FFC4C4C4" Padding="10" />
                                    <TextBlock Text="{Binding Distance}" TextWrapping="Wrap" FontSize="16" Foreground="#FFC4C4C4" Padding="10" />
                                    <TextBlock Text="{Binding DistanceInMinutes}" TextWrapping="Wrap" FontSize="16" Foreground="#FFC4C4C4" Padding="10" />
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

我在.cs文件中有一个更改事件

private void CouponListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (CouponListBox.SelectedIndex == -1)
                return;
            // Navigate to the new page
            System.Diagnostics.Debug.WriteLine("this is a test::" + CouponListBox.SelectedItem.ToString());
            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + CouponListBox.SelectedIndex, UriKind.Relative));

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

在DetailsPage中我可以打印项目索引。但我想要的是在URL中传递的客户ID,如

"/DetailsPage.xaml?selectedItem=" + CouponListBox.SelectedIndex + "&customerId=" + couponId

任何人都可以告诉我应该在我的XAML文件中包含customerId吗?以及我可以在函数中调用它的人。

谢谢你, KARTHIK

1 个答案:

答案 0 :(得分:6)

使用此:

if (this.NavigationContext.QueryString.ContainsKey("customerId")) 
{
    string customerId = this.NavigationContext.QueryString["customerId"];
}

if (this.NavigationContext.QueryString.ContainsKey("selectedItem")) 
{
    string selectedItem = this.NavigationContext.QueryString["selectedItem"];
}

在usercontrol的Loaded或其他适当的事件中。