Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改图像源,在GUI中显示它,然后运行一些代码?_C#_Wpf_Multithreading_Image_User Interface - Fatal编程技术网

C# 如何更改图像源,在GUI中显示它,然后运行一些代码?

C# 如何更改图像源,在GUI中显示它,然后运行一些代码?,c#,wpf,multithreading,image,user-interface,C#,Wpf,Multithreading,Image,User Interface,我的WPF中有一个图像控件(它基本上是整个窗口)。 当用户输入错误时,我希望更改图像,甚至摇动屏幕,等待一秒钟(使用Thread.Sleep()),然后将图像更改回原始图像 问题是:即使我在摇动屏幕之前更改了函数中的图像,但在所有函数执行后图像仍然会更改 我怎样才能实现我的目标 这就是我所尝试的: //Change Diary Background BitmapImage bitmap = new BitmapIma

我的WPF中有一个图像控件(它基本上是整个窗口)。 当用户输入错误时,我希望更改图像,甚至摇动屏幕,等待一秒钟(使用Thread.Sleep()),然后将图像更改回原始图像

问题是:即使我在摇动屏幕之前更改了函数中的图像,但在所有函数执行后图像仍然会更改

我怎样才能实现我的目标

这就是我所尝试的:

                    //Change Diary Background
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.UriSource = new Uri(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) + @"\lib\ViewLogin\ViewLoginIncorrect.png");
                    bitmap.EndInit();
                    imgDiary.Source = bitmap;

                    Thread.Sleep(1000);

                    //Shake Screen
                    this.Left = this.Left - 20;
                    Thread.Sleep(50);
                    this.Left = this.Left + 20;

                    txtMasterKey.Focus();
只有在一切完成后,我的形象才会改变。
Thread.sleep阻止ui线程,该线程将更改任何ui

这是你的问题

使您的方法异步

将thread.sleep(n)替换为wait task.delay(n)


其中n是您在那里的数字。

Thread.Sleep会阻止UI线程。声明您的方法
async
并将
Thread.Sleep(…)
替换为
wait Task.Delay(…)