草庐IT

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

coder 2024-06-16 原文

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

这是枢轴项

<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 Phone 7 如何在枢轴项之间切换时删除 "stuttering"的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  5. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  6. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  7. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  8. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  9. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  10. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

随机推荐