Karthik
Karthik

Reputation: 1091

Windows Phone 7 Navigation passing parameter

I have a list box like below.

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

And I have a Change Event in .cs file which is

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

In DetailsPage I could able to print the item Index. But What I want is Customer ID passed in the URL like

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

Can anyone please tell me where I should include customerId in my XAML file? and that who can I call it in the function.

Thank you, Karthik

Upvotes: 2

Views: 3609

Answers (1)

Emond
Emond

Reputation: 50672

Use this:

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

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

in the usercontrol's Loaded or other appropriate event.

Upvotes: 6

Related Questions