简体   繁体   中英

WP7: Executing a NavigationService.Navigate from a MenuItem click handler from a Context menu on WP7

I have a context menu in my app and when I handle the MenuItem clicked, I want to navigate to another page. The issue I am having is that the navigation works but I don't see the other page load since the context menu remains open for the length of the click. When I hit back, the context menu closes and then I see the page I navigated to. What's the correct way to handle this? It's almost like I need to tell the ContextMenu to close when I handle the click and then navigate to the page I want.

updated with my handler code:

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    MenuItem menuItem = sender as MenuItem;
    if (menuItem != null)
    {   
        App.appData.URL = menuItem.Header.ToString();
        NavigationService.Navigate(new Uri("/BrowserPage.xaml", UriKind.Relative));            
    }
}

XAML Code:

<local:MyListBox x:Name="messageListBox" 
                                 ItemsSource="{Binding ChannelMessages}" 
                                 MaxHeight="480"
                                 >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <toolkit:ContextMenuService.ContextMenu>
                                    <toolkit:ContextMenu ItemsSource="{Binding URLs}">
                                        <toolkit:ContextMenu.ItemTemplate>
                                            <DataTemplate>
                                                <toolkit:MenuItem Header="{Binding}" Click="MenuItem_Click"/>
                                            </DataTemplate>
                                        </toolkit:ContextMenu.ItemTemplate>
                                    </toolkit:ContextMenu>
                                </toolkit:ContextMenuService.ContextMenu>
                                <TextBlock x:Name="MessageTextbox" Text="{Binding MessageFrom}" TextWrapping="Wrap">
                                        <TextBlock.Foreground>
                                            <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
                                        </TextBlock.Foreground>
                                </TextBlock>
                                <TextBlock x:Name="FromTextBox" Text="{Binding MessageText}" Margin="0,0,0,19" Width="456" FontSize="21.333" TextWrapping="Wrap"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </local:MyListBox>

Any ideas?

Thanks

在点击事件处理程序中设置e.Handled = true是否有帮助?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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