c# - Silverlight:当我调用 NavigationService.Navigate 时布局发生变化

标签 c# silverlight xaml windows-phone-7

这是一个非常奇怪的错误。我不知道为什么会这样。我知道将它张贴在这里有点遥不可及,但我没有其他想法。

我有两个用作菜单的 ListBox

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

他们两个都存在这个错误。

当任一菜单上的选择发生变化时,将调用此方法:

    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 this line, the problem goes away:
        NavigationService.Navigate(page);

        ListBox selectedBox = (ListBox)sender;
        selectedBox.SelectedIndex = -1;
    }

如果我注释掉 NavigationService.Navigate() 行,问题就会消失。如果我用不同的 URI 替换该行,问题仍然存在。

大约 70% 的时间,当我点击菜单项时,内容会跳到整个页面。 (剩下的 30%,没有 bug 发生。)它发生得太快,看不到真正发生了什么,但不同的 UI 元素相互重叠。

这只会在我在应用程序的生命周期中第一次单击这些菜单中的某些内容时发生。如果我点击“返回”然后再次选择一个菜单项,问题就不会发生。

这里会发生什么?我真的不知道。代码隐藏没有 OnNavigatedFrom 方法,所以我认为这不是问题。

我正在为 Windows Phone 7 使用 Silverlight

更新:奇怪的是,我似乎无法在调试器中重现这一点 - 只有在部署应用程序并在未连接的模拟器中运行它之后。 ???

更新 2:当从按钮的 Click 事件处理程序调用 NavigationService.Navigate() 时,也会出现该错误:

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

看起来这个错误与导航有关,而不是用于触发调用的 UI 元素。

更新 3:更古怪。附加调试器后仍然无法重现应用程序。如果我让加载进度条总是折叠起来,这个错误就消失了:

                <ProgressBar x:Name="LoadingProgressBar"
                 IsIndeterminate="True"
                 Visibility="Collapsed"
                 Style="{StaticResource PerformanceProgressBar}"
                 VerticalAlignment="Top"/>

或者,在代码隐藏中注释掉这一行可以使错误消失:

LoadingProgressBar.Visibility = Visibility.Collapsed;

我真的不明白这是怎么回事。当从页面导航时,该行代码不会执行。

这是被搞砸的控件的完整 XAML:

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

最佳答案

问题在于您对 Indeterminate ProgressBar 的使用。它的所有动画都在 UI 线程上完成,而不是像通常的做法那样在 Compositor 线程上完成。由于您已经在使用 Windows Phone 工具包,因此您可以轻松地将 ProgressBar 替换为该工具包提供的 PerformanceProgressBar。这应该可以解决您的问题。

关于c# - Silverlight:当我调用 NavigationService.Navigate 时布局发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4565535/

相关文章:

wpf - 自定义控件的内容无法绑定(bind)到控件的父级

c# - 如果 C# 不是可选的,为什么它会中断?

c# - 为什么会调用 OnlyOnCanceled 延续?

c# - 程序已退出,代码为 -1073610751 (0xc0020001)

c# - 哪一个是清除字符串生成器的性能明智的?

silverlight - 遵循 MVVM 模式动态创建控件

silverlight - 如何将 Silverlight DatePicker 控件设置为只读?

silverlight - 在 Silverlight 应用程序中检测用户不活动的最佳方法是什么?

silverlight - 如何将 ResourceDictionary (Styles.xaml) 中 Silverlight ListItem DataTemplate 中的按钮与处理程序连接起来?

c# - 在 XAML 中操作静态资源