c# - Windows Phone 7 导航传递参数

标签 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?以及我可以在函数中调用谁。

谢谢, 卡尔提克

最佳答案

使用这个:

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

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

在用户控件的 Loaded 或其他适当的事件中。

关于c# - Windows Phone 7 导航传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971761/

相关文章:

c# - 有没有更优雅的方法使用 system.text.json 从 JSON 对象获取特定值

c++ - 如何在 VS 2010 中生成均匀分布的随机数?

c++ - Visual Studio 2010 找不到解决方案的局部变量以外的任何定义

visual-studio-2010 - 为什么有时我在VS2010中调试程序,我的ctrl被按下了?

c# - 在 Windows Phone 7 的 C# 中将 ArrayOfString 转换为 List<string>

windows-phone-7 - Windows Phone 7 + Canvas + 手势 + OnDrag 事件 + 限制平移/拖动

c# - 让相机在人像模式下正常工作

c# - T4文本模板: method to return html expression

c# - 如何从用户 View 中隐藏我安装的 dll

c# - 带有 Interop.Word 的斜体内联字