如何在后台运行任务时在WPF中旋转图像?
问题描述:
我有一个WPF应用程序,其中一个启动按钮执行一些Web服务调用,它返回一些结果,我使用oxyplot在图上绘制。我在UI上有一个图像,我希望将其用作“微调器”,直到任务完成。如何在后台运行任务时在WPF中旋转图像?
我可以使用按钮点击路由事件旋转我的图像,但我希望它保持旋转,直到我的Web服务调用完成。有什么建议么?
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="capIcon" Storyboard.TargetProperty="RenderTransform.Angle" By="90" Duration="0:0:1" />
<DoubleAnimation Storyboard.TargetName="capIcon" Storyboard.TargetProperty="RenderTransform.Angle" By="90" Duration="0:0:1" />
<DoubleAnimation Storyboard.TargetName="capIcon" Storyboard.TargetProperty="RenderTransform.Angle" By="90" Duration="0:0:1" />
<DoubleAnimation Storyboard.TargetName="capIcon" Storyboard.TargetProperty="RenderTransform.Angle" By="90" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
,这里是我的按钮单击方法:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (!validateThreadCount())
return;
RunTimes.runTimes.Clear();
//this.Plot.InvalidateFlag = 1;
int threads = Convert.ToInt16(this.ThreadCount.Text);
Task[] tasks = new Task[threads];
for (int i = 0; i < threads; i++)
{
DealClient dealClient = new DealClient();
var t = Task.Factory.StartNew(() => dealClient.NewDeal(1));
tasks[i] = t;
}
Task.WaitAll(tasks);
var points = new List<ScatterPoint>();
for (int i = 0; i < RunTimes.runTimes.Count; i++)
{
points.Add(new ScatterPoint(RunTimes.runTimes[i].Milliseconds, i, 3));
}
this.pts.ItemsSource = points;
}
答
你需要使用一个异步和等待方法,可处理该事件。我没有足够的代码来给你正确的名字,但是这是我会怎样处理这个问题
Private async Task MethodThatHandlesEvent()
{
await Task.Run(() =>
{
RunWebService();
While(WebServiceIsRunning)
{MakeImagesSpin();}
});
}
Private async void MyButton_Click()
{
await MethodThatHandlesEvent();
}
谢谢,这帮助了,现在我只需要弄清楚如何从C#旋转图像 – user6849787