Web 服务调用返回时加载动画冻结

发布于 2024-07-25 17:08:59 字数 305 浏览 5 评论 0原文

我有一个 Silverlight 2 应用程序,其中有一条包含动画的“正在加载数据...”消息。 当 Web 服务返回数据并且数据正在加载到可观察集合中时,动画会冻结(在加载数据时这可能会持续几秒钟,给人一种可能出现问题的印象。

)数据绑定到视图中的项目控件。

我假设发生的情况是,当数据被添加到可观察集合中时,正在为数据绑定控件构建可视化树 - 这发生在 UI 线程上。

无论如何,我是否可以降低数据加载的优先级,以便 ui 线程上的动画可以继续 - 即使 UI 线程上正在完成大量数据工作?

谢谢 迈克尔

I have a Silverlight 2 application that has a "Loading Data..." message that contains an animation. When the web service returns the data and the data is loading in to an observable collection -- the animation freezes (this can be for for several seconds while the data is loading and give the impression that something moght be wrong.)

The observable collection that is databound to an items control in a view.

I assume what is happening is when the data is being added to the observable collection the visual tree is being built for the databound control - which happens on the UI thread.

Is there anyway for me to lower the priority of the data loading so the animation on the ui thread can continue -- even when there is a ton of data work being done on the UI thread?

thanks
Michael

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

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

发布评论

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

评论(4

雅心素梦 2024-08-01 17:08:59

我不知道你是否得到了答案,但我来了。

尝试使用 BackgroudWorker 将重型指令放在那里。

这是一个很好的例子: http://msdn.microsoft .com/en-us/library/system.componentmodel.backgroundworker.aspx

如果您有任何问题,请告诉我,或者如果您已经解决了此问题,请告诉我。

I don't know if you got the answer for this or not but here i go.

Try using a BackgroudWorker to put the heavy duty instructions in there.

Here is a very good example: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Let me know if you have any issues, or let me know if you already solved this problem.

情未る 2024-08-01 17:08:59

我一直在使用具有不确定状态的进度控件,然后将其放入项目控件顶部具有半透明背景的控件中。 我还没有看到它在进行 Web 服务调用时停止动画,返回对象被处理到 ViewModel 中,并且 ItemControl 正在渲染更新的视图模型。

我认为您可以以非常简单的方式自定义进度栏模板,并将“正在加载数据...”消息添加为 TextBlock。 我的进度控件上方也有一条正在加载...消息。

I have been using a Progress Control with Indeterminate state, and then put that in a control with a semi-transparent background over the top of the items control. I have not seen it stop animating while the web service call is made, the return objects are processed into a ViewModel and the ItemControl is rendering the updated view model.

I think you can customize the progress bar template in a pretty straightforward way as well as adding the Loading Data... message as a TextBlock. I have a Loading... message above the Progress control as well.

糖粟与秋泊 2024-08-01 17:08:59

你说得对,回调发生在 UI 线程上。 要解决您的问题,您可以使其异步,以便它发生在后台线程上,然后构建一个新的可观察集合并在一次操作中替换现有的集合。
例如:

private SynchronizationContext _uiThread = SynchronizationContext.Current;

public void UpdateUIAsynchronously(ICollection<Data> data)
{
ObservableCollection<Data> temp = new ObservableCollection<Data>();
foreach(Data currentData in data)
{
temp.Add(currentData);
}

_uiThread.Post(delegate(object o) { myDataGrid.ItemsSource = temp }, null);
}

在我的开发中,我发现对于 DataGrid,我可以在 UI 线程上清除并添加新元素,而不会影响性能。 也许你可以重构你的 UI 代码?

You're right in saying that the callback happens on the UI thread. To solve your issue you can make it asynchronous so that it happens on a background thread and then build a new observable collection and replace the existing one in one operation.
For example:

private SynchronizationContext _uiThread = SynchronizationContext.Current;

public void UpdateUIAsynchronously(ICollection<Data> data)
{
ObservableCollection<Data> temp = new ObservableCollection<Data>();
foreach(Data currentData in data)
{
temp.Add(currentData);
}

_uiThread.Post(delegate(object o) { myDataGrid.ItemsSource = temp }, null);
}

In my development I found that for a DataGrid I could just clear and add the new elements on the UI thread without performance hit. Maybe you could refactor your UI code?

慈悲佛祖 2024-08-01 17:08:59

您的动画未运行,因为您的 JS 正在处理返回的数据。 (单线程浏览器。)

解决方案:您的 JS 加载 sw 需要定期屈服于浏览器,以使浏览器能够执行一些动画。

请参阅我的答案 为什么 jQuery Ajax 在 IE7 上如此慢? 了解有关如何操作的更多详细信息。

Your animation isn't running because your JS is dealing with the returned data. (Single threaded browser.)

Solution: your JS loading sw needs to yield to the browser periodically to enable the browser to do some animation.

See my SO answer Why is jQuery Ajax so slow on IE7? for more details on how to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文