This is a really weird mistake. I have no idea why this could happen. I know that his placement here is a little long, but I have no other ideas.
I have two ListBox that act as menus.
<ListBox Margin="56,8,15,0" FontSize="64" ItemsSource="{Binding FavoriteSections}" SelectionChanged="MenuList_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="Remove" Click="FavoritesContextMenuItem_Click" /> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> <TextBlock Text="{Binding DisplayName}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <ListBox x:Name="sectionList" Margin="56,8,15,0" FontSize="64" SelectionChanged="MenuList_SelectionChanged" ItemsSource="{Binding SectionViewModels}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="Add to favorites" Click="SectionContextMenuItem_Click" /> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> <TextBlock Text="{Binding DisplayName}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
An error exists for both of them.
When a selection changes in any of the menus, this method is called:
void MenuList_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count == 0) { return; } Uri page = null; object selected = e.AddedItems[0]; if (selected is NavigableItem) { NavigableItem selectedItem = (NavigableItem)selected; page = selectedItem.Page; } else if (selected is SectionViewModel) { SectionViewModel selectedVM = (SectionViewModel)selected; page = selectedVM.Section.Page; } Debug.Assert(page != null, "What is the type of `selected`?");
If I comment out the line NavigationService.Navigate() , the problem goes away. If I replace the string with a different URI, the problem will remain.
In about 70% of cases, when I click on a menu item, the content skips across the page. (The remaining 30%, the error does not occur.) This happens too quickly to see what is happening, but different elements of the user interface overlap.
This only happens the first time I click something on these menus during the life of the application. If I press "back", then select the menu item again, the problem will not be.
What could be here? I really have no idea. The code behind does not have an OnNavigatedFrom method, so I don't think this is a problem.
I am using Silverlight for Windows Phone 7
Update . Mysteriously, I cannot reproduce this in the debugger - only after deploying the application and running it in the emulator without connecting. ???
Update 2 : an error occurs when NavigationService.Navigate() is called from the button's Click event handler:
<Button Content="Foo" Click="Button_Click" Grid.Row="0"/> private void Button_Click(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/Views/sections.xaml?section=43", UriKind.Relative)); }
It seems that the error is related to navigation, and not to the user interface element used to make the call.
Update 3 : more weird. I still cannot play the application while the debugger is connected. If I make the progress bar always fail, the error disappears:
<ProgressBar x:Name="LoadingProgressBar" IsIndeterminate="True" Visibility="Collapsed" Style="{StaticResource PerformanceProgressBar}" VerticalAlignment="Top"/>
Alternatively, commenting out this line in encoding, the error disappears:
LoadingProgressBar.Visibility = Visibility.Collapsed;
I really donβt understand what is going on here. This line of code is not executed when the page moves out.
Here is the full XAML of the control that got confused:
<ProgressBar x:Name="LoadingProgressBar" IsIndeterminate="True" Visibility="Collapsed" Style="{StaticResource PerformanceProgressBar}" VerticalAlignment="Top"/> <TextBlock x:Name="DownloadFailed" Visibility="Collapsed" Style="{StaticResource disabledText}" Margin="56,8,8,-8" > FooBar.com could not be reached. Do you have a network connection? </TextBlock> <ListBox x:Name="sectionList" Margin="56,8,15,0" FontSize="64" SelectionChanged="MenuList_SelectionChanged" ItemsSource="{Binding SectionViewModels}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="Add to favorites" Click="SectionContextMenuItem_Click" /> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> <TextBlock Text="{Binding DisplayName}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </controls:PivotItem>