0

I'm working on a Windows Phone App. I have a very performance intensive method which takes several seconds until the operation is done.

When the method is called I want to show an animated loading symbol which is defined in the xaml of my view. When the operation is finished it should disappear. I set the loading symbol to visible in the first line of this method.In the last line I set the visibility to collapsed.

The problem is that at first the whole code behind will be executed. Unfortunately nothing is to be seen, because the the visibiliy is set to visible after the code behind operations are executed and in the same moment its set to collapsed.

Has anybody an idea how to solve this problem? Thanks so much in advance.

4

2 回答 2

2

您遇到的问题是您在主(UI)线程上调用您的方法。这意味着您的方法阻止了 UI 刷新,也意味着(如您所述)在 UI刷新时,您已经再次隐藏了图标。

您需要做的是在后台线程上调用您的方法(有多种方法可以处理此问题)。您需要将 UI 更新推送到 UI 线程(使用Dispatcher.Invoke),但您的方法的其余部分将在单独的线程上运行。

您还需要使用某种回调 - 可能是自定义事件 - 以便您的 UI 线程知道后台线程何时完成。

于 2013-04-04T14:43:29.573 回答
-1

在没有看到代码的情况下很难确定,但是如果您在设置繁忙指示器后使用调度程序运行密集代码,这将允许在运行代码之前更改 ui 线程时间。

一个例子

//This assumes you are binding in xaml to the isbusy and it implements INotifyPropertyChanged
IsBusy = true;
Dispatcher.BeginInvoke(()=>{ //...performance intense here
});

Dan Puzey 说的是对的。如果出于某种原因需要,您应该只在 UI 线程上运行此逻辑。即便如此,也要小心这一点,因为它会导致糟糕的用户界面体验。

您可以完成此操作并在需要时仍然启动调度程序的一种方法是将调度程序的副本传递到后台。

ThreadPool.QueueUserWorkItem (d => {
    //...performance intense here
    Dispatcher dispatcher = d as Dispatcher;
    if(dispatcher != null){
         dispatcher.BeginInvoke()()=>{//...ui updates here }
    }

}, Dispatcher.CurrentDispatcher);//make sure this is called from your UI thread or you may not end up with the correct dispatcher
于 2013-04-04T14:46:25.030 回答