使用异步等待在WPF WizardControl上进行自定义忙覆盖 [英] Custom Busy overlay on WPF WizardControl using async await
问题描述
我需要在WPF向导控件上加载叠加层.我正在使用wpf扩展takedlit中的busyIndicator工具.
I need load a overlay on a WPF wizardcontrol. I am using a busyIndicator tool from the wpf extended tooklit.
异步代码等待,但是gui线程锁定.我正在尝试在await调用函数时添加一条请稍候消息
The code for async await works but the gui thread locks . I am trying add a please wait message when the await calls the function
private async void Button1_Click(object sender, RoutedEventArgs e)
{
BusyIndicator.IsBusy = true;
BusyIndicator.IsEnabled = true;
BusyIndicator.BusyContent = "Please wait while Site is provisioned";
await Task.Run(() =>
{
LongRunningFunction();
});
BusyIndicator.IsBusy=false;
}
BusyIndicator的XAML如下.
The XAML for the BusyIndicator is as below.
<xctk:BusyIndicator x:Name="BusyIndicator" IsBusy="False" BusyContent="Please Wait">
</xctk:BusyIndicator>
LonRunningFunction是一个Web服务调用,它不会更新UI,只会返回Bool值
The LonRunningFunction is a Webservice call which does not update the UI only returns a Bool value
public static bool LongRunningFunction(string URL)
{
bool IsPresent = CallWebservice()
return IsPresent;
}
问题
1)BusyIndicator似乎没有在异步调用之前触发,而是在LongRunning任务完成时似乎触发了
1) The BusyIndicator does not seem to fire before the async call instead it seems to be fire when the LongRunning task completes
2)使用异步和等待时调用gui覆盖的正确过程是什么?
2) What is the correct process to call a gui overlay when async and await is used.
推荐答案
这是我通过异步调用解决问题的方法.
上下文:
在这里,我正在使用MvvM
向您展示使用WPF
This is the way I tackled the problem with asynchronous calls.
Context:
Here I am using MvvM
to show you good practice when working with WPF
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
class VM
{
Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;
//Time consuming operation
private void LongTask()
{
Thread.Sleep(5000);
//in here if you need to send something to the UI thread like an event use it like so:
_dispatcher.Invoke(new Action(() =>
{
//some code here to invoke an event
if (ComponentsLoaded != null)
ComponentsLoaded(this, new EventArgs { });
}));
}
private ICommand _command;
//This is the command to be used instead of click event handler
public ICommand Command
{
get { return _command; }
private set { _command = value; }
}
//method associated with ICommand
void commandMethod(object parameter)
{
Busy = true;
ThreadPool.QueueUserWorkItem(new WaitCallback(multiThreadTask));
Busy = false;
}
//the task to be started on another thread
void multiThreadTask(object parameter)
{
LongTask();
}
public event EventHandler ComponentsLoaded;
}
这是在WPF中使用多个线程时使用的方式.
您仍然可以在代码中使用它,只需实例化Dispatcher
,您就可以开始使用
.
如果您需要更多信息,请告诉我们. HTH
This is what I use when working with multiple threads in WPF.
You can still use this in the code-behind just instantiate the Dispatcher
and you're good to go.
If you need any more info just let us know. HTH
这篇关于使用异步等待在WPF WizardControl上进行自定义忙覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!