c# - Windows Phone 7 如何在枢轴项之间切换时删除 "stuttering"

标签 c# windows silverlight windows-phone-7

当切换到加载数据的数据透视表项时,我在从一项切换到另一项时遇到卡顿现象。我已将数据加载分离到一个单独的线程中,这很有帮助,但我仍然遇到一些糟糕的性能....想知道你们是否有任何想法....

这是枢轴项

<Grid x:Name="LayoutRoot" Background="Transparent">

    <!--Pivot control-->
    <controls:Pivot Name="panCorals" Title="Corals" Foreground="#01487e" 
        SelectionChanged="panCorals_SelectionChanged">

        <controls:Pivot.Background>
            <ImageBrush ImageSource="PivotBackground.png"/>
        </controls:Pivot.Background>

        <!--Search Corals-->
        <controls:PivotItem Header="Search" Foreground="#01487e">

            <Grid>

                <toolkit:PerformanceProgressBar Name="SearchCoralsProgressBar" HorizontalAlignment="Left" 
                        VerticalAlignment="Top" Width="466" 
                        IsEnabled="{Binding IsSearchLoading}" IsIndeterminate="{Binding IsSearchLoading}" />

                <StackPanel>
                    <TextBox Name="txtSearchTerm" KeyDown="txtSearchTerm_KeyDown" />
                    <ListBox Name="lbSearchCorals" Margin="0,0,-12,0" ItemsSource="{Binding SearchCorals}" 
                         SelectionChanged="lbSearchCorals_SelectionChanged">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                    <Image Source="{Binding MainImageURI}" Height="100" Width="100" Margin="12,0,9,0" />
                                    <StackPanel Width="311">
                                        <TextBlock Text="{Binding CommonName}" Foreground="#112d42"  TextWrapping="NoWrap"  Style="{StaticResource PhoneTextTitle2Style}"/>
                                        <TextBlock Text="{Binding ScientificName}" Foreground="#112d42" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>
            </Grid>

        </controls:PivotItem>


        <!--Top Corals-->
        <controls:PivotItem Header="Top" Foreground="#01487e" VerticalAlignment="Top" >

            <Grid>

                <toolkit:PerformanceProgressBar Name="TopCoralsProgressBar" HorizontalAlignment="Left" 
                        VerticalAlignment="Top" Width="466" 
                        IsEnabled="{Binding IsTopLoading}" IsIndeterminate="{Binding IsTopLoading}" />


                <ListBox Name="lbTopCorals" Margin="0,0,-12,0" ItemsSource="{Binding TopCorals}" SelectionChanged="lbTopCorals_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                <Image Source="{Binding MainImageURI}" Height="100" Width="100" Margin="12,0,9,0" />
                                <StackPanel Width="311">
                                    <TextBlock Text="{Binding CommonName}" Foreground="#112d42"  TextWrapping="NoWrap"  Style="{StaticResource PhoneTextTitle2Style}"/>
                                    <TextBlock Text="{Binding ScientificName}" Foreground="#112d42" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

                <StackPanel Margin="10,50,0,0" Orientation="Horizontal">
                    <TextBlock x:Name="tbProgress"/>
                </StackPanel>

            </Grid>

        </controls:PivotItem>

        <!--New Corals-->
        <controls:PivotItem Header="New">
            <Grid>

                <toolkit:PerformanceProgressBar Name="NewCoralsProgressBar" HorizontalAlignment="Left" 
                            VerticalAlignment="Top" Width="466" 
                            IsEnabled="{Binding IsNewLoading}" IsIndeterminate="{Binding IsNewLoading}" />

                <ListBox Name="lbNewCorals" Margin="0,0,-12,0" ItemsSource="{Binding NewCorals}" SelectionChanged="lbNewCorals_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                <Image Source="{Binding MainImageURI}" Height="100" Width="100" Margin="12,0,9,0" />
                                <StackPanel Width="311">
                                    <TextBlock Text="{Binding CommonName}" Foreground="#112d42"  TextWrapping="NoWrap"  Style="{StaticResource PhoneTextTitle2Style}"/>
                                    <TextBlock Text="{Binding ScientificName}" Foreground="#112d42" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

            </Grid>
        </controls:PivotItem>


    </controls:Pivot>
</Grid>

以及背后的代码......

private void panCorals_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (panCorals.SelectedIndex)
        {
            case 0:     //search corals

                break;
            case 1:         //top corals
                if (!App.vmCoral.IsTopDataLoaded)
                {
                    App.vmCoral.IsTopLoading = true;
                    if (App.HasConnectivity)
                    {
                        //get corals from web
                        bw.DoWork += new DoWorkEventHandler(bw_DoWorkTopCoralsWeb);

                        if (bw.IsBusy != true)
                        {
                            bw.RunWorkerAsync();
                        }

                    }
                    else
                    {
                        //get saved corals from device
                        bw.DoWork += new DoWorkEventHandler(bw_DoWorkTopCoralsSaved);

                        if (bw.IsBusy != true)
                        {
                            bw.RunWorkerAsync();
                        }
                    }
                }
                break;
            case 2:         //new corals

                if (!App.vmCoral.IsNewDataLoaded)
                {
                    App.vmCoral.IsNewLoading = true;
                    if (App.HasConnectivity)
                    {
                        //get corals from web
                        bw.DoWork += new DoWorkEventHandler(bw_DoWorkNewCoralsWeb);

                        if (bw.IsBusy != true)
                        {
                            bw.RunWorkerAsync();
                        }

                    }
                    else
                    {
                        //get saved corals from device
                        bw.DoWork += new DoWorkEventHandler(bw_DoWorkNewCoralsSaved);

                        if (bw.IsBusy != true)
                        {
                            bw.RunWorkerAsync();
                        }
                    }
                }

                break;
            default:

                break;
        }

    }

最佳答案

您发布的代码本身并没有什么特别的问题。最有可能发生的情况是,当您在数据透视项之间切换时,您的后台工作线程正在完成,这反过来会更新您的列表绑定(bind)到的可观察集合。此外,您的列表包含图像,如果您绑定(bind)到需要下载图像的 Web URL,也会导致性能问题。

有几件事需要检查:

  1. 如果您的列表很长,请确保您正在使用虚拟堆栈面板(这是默认设置,但请确保您没有在任何地方更改它)。
  2. 考虑使用 SL 工具包中的 LongListSelector 控件,它比默认的 ListBox 具有更好的 UI 和数据虚拟化支持
  3. 如果您要将图像绑定(bind)到网址 (http://blogs.msdn.com/b/delay/archive/2010/09/02/keep-a-low-profile-lowprofileimageloader-helps-the-windows-phone-7-ui-thread-stay-responsive-by-loading-images-in-the-background.aspx),请检查低调图像加载器。
  4. 在您的 App.xaml.cs 中启用“EnableRedrawRegions”调试指示器。检查在切换枢轴项目时是否没有任何内容导致区域被重绘(由屏幕的一部分快速闪烁不同的颜色表示)。如果是,请考虑使用 BitMapCache。
  5. 如果您的异步进程正在下载一个长列表,请考虑在后台线程中分解该列表,并仅将它们以小块的形式分派(dispatch)到 UI 线程,每个 block 之间的延迟很小。

关于c# - Windows Phone 7 如何在枢轴项之间切换时删除 "stuttering",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6932170/

相关文章:

c# - 在字典内部的字典中添加键值对

windows - SendMessage 或 SendMessageTimout 线程安全吗?

.net - 用户在采用 Silverlight 时是否有问题?

c# - 异步foreach

c# - 在 itextsharp 中将自定义字体应用于 html

c# - 如何从 .net 应用程序针对 sqlserver 运行 sql 并像使用 SQL Management Studio 一样获得输出?

java - 使用 Maven 命令执行特定的 JUnit 测试用例

windows - 64位Windows平台还不成熟吗? (即使将 32 位二进制文​​件与在其上运行的 64 位二进制文​​件进行比较)

silverlight - Chrome 无法解压缩 gzip,内容长度

silverlight - 对图像 wp7 实现缩放