单击图像后C#短暂延迟显示新源

时间:2017-02-26 16:11:49

标签: c# wpf

我有一个Windows.Controls.Image,点击后需要更改其来源并显示x毫秒,然后导航到新的页面。我在更改源代码后尝试使用Thread.Sleep(200),但无论我如何强制重绘已更改的源都没有显示,然后当睡眠结束时它立即更改了页面。有谁知道我怎么做到这一点?干杯!

private void ButtonClicked(object sender, MouseButtonEventArgs e)
    {
         Image button = (Image)sender;
         buttons[button.Name].Source = ImageResizer.ResizeImage(System.Drawing.Image.FromFile(@"D:\buttons\" + button.Name + ".png"), buttonSize);
         NavigationService.Navigate(new UpgradeQuiz());
    }

1 个答案:

答案 0 :(得分:1)

您可以异步等待:

await Task.Delay(2000);

但你应该让你的处理程序async

private async void ButtonClicked(object sender, MouseButtonEventArgs e) // async after private
{
     Image button = (Image)sender;
     buttons[button.Name].Source = ImageResizer.ResizeImage(System.Drawing.Image.FromFile(@"D:\buttons\" + button.Name + ".png"), buttonSize);
     await Task.Delay(2000);
     NavigationService.Navigate(new UpgradeQuiz());
}