有没有办法采用创建 UI 元素并使其可取消的方法?

发布于 2024-10-22 20:36:48 字数 509 浏览 1 评论 0原文

我有一个在 WP7 应用程序中输入图表页面时被调用的方法。它生成一个对象列表并填充一个列表框。每个ListBoxItem的内容是一个包含10列数据的Grid。即使有 1000-2000 个项目,列表的生成速度也非常快。但是,一旦该方法开始构建网格并将其添加到列表框,它就会变得相对慢得多。现在,我的意思是,它只占用设备的时间只有第二代同类应用程序的一半。 iPod 触摸。所以性能很棒——只要用户想要数据图表。

如果用户点击“开始”按钮,应用程序就会退出,所以这不是问题。我担心的是当用户退出到上一页时。应用程序只是等待该方法运行。我注意到 Kindle 应用程序等更主流的应用程序中有类似的行为。但我对 MarketPlace 商店没有那种影响力!我确实有一个持续运行的进度条,因此行为是相同的。

出于对被 MarketPlace 拒绝的担忧,我尝试将该方法放入 BackgroundWorker 进程中,但失败了,因为它是在创建 UI 元素时遇到瓶颈所在并且在 UI 线程上运行,因此出现访问错误。有没有办法采用创建 UI 元素(例如 Grid)的方法,并使其可取消?

I have a method that gets called when entering a chart page in my WP7 app. It generates a List of objects and populates a ListBox. The content of each ListBoxItem is a Grid with 10 columns of data. The List gets generated incredibly quickly, even with 1000-2000 items. But as soon as the method starts building Grids and adding them to the ListBox it gets relatively much slower. Now, by this I mean it only ties up the the device for half as long as a comparable app on my 2nd gen. iPod Touch. So performance is great - as long as the user wants the data chart.

If the user hits the Start button the app exits so that's not a problem. My concern is when the user backs out to the previous page. The app just waits until the method has run. I notice similar behavior in more mainstream apps like the Kindle app. But I don't have that kind of clout with MarketPlace store! I do have a progress bar that keeps running so the behavior is the same.

Out of concern for being rejected by MarketPlace I tried putting the method into a BackgroundWorker process but that fails because it's in creating the UI elements that is where the bottleneck is and that is running on the UI thread so I get access errors. Is there a way to take a method that creates UI elements, such as a Grid, and make it cancelable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄情伤 2024-10-29 20:36:48

您是否在代码中手动创建每个 ListBoxItem 中的 UI 元素?如果是这样,您会发现使用数据绑定可以提高性能,因为 ListBox 使用 VirtualizingStackPanel 作为项目容器,因此它实际上只会为足够的元素创建 UI 元素。看到并立即滚动到。当用户开始滚动时创建其他元素。 Silverlight for Windows Phone 性能团队在 列表框性能

如果数据集特别大,您可能会通过使用数据虚拟化(或相反)发现进一步的性能改进,正如 Peter Torr 在他的 在 Windows Phone 7 中虚拟化数据帖子。

Are you creating the UI elements within each ListBoxItem manually in code? If so, you will find increased performance by using databinding instead because the ListBox uses the VirtualizingStackPanel as the items container, so it will only actually create UI elements for enough elements to be seen and to scroll to immediately. Other elements are created when the user starts to scroll. The Silverlight for Windows Phone Performance Team have a great post on ListBox Performance.

If the dataset is particularly large you may find further peerformance improvements by using data virtualization as well (or instead) as Peter Torr explains in his Virtualizing Data in Windows Phone 7 post.

做个ˇ局外人 2024-10-29 20:36:48

您应该使用BackgroundWorker。当您需要更新 UI 时,请使用以下代码...

Dispatcher.BeginInvoke(() =>
{
   textBlock.Text = "some text";
   etc
   etc
}

You should use the BackgroundWorker. When you need to update the UI use the following code...

Dispatcher.BeginInvoke(() =>
{
   textBlock.Text = "some text";
   etc
   etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文