在WPF应用程序中更改Button的BackgroundImage(Changing Button's BackgroundImage in WPF app)
在我的c#app中,我试图更改按钮的背景图像并在几秒钟后将其更改回来。 但是,直到计时器启动后才会更改背景图像,然后在您看到更改之前立即将其更改回原始图像。
private void button1_Click(object sender, EventArgs e) { myImage.BackgroundImage = Properties.Resources.newImage; System.Threading.Thread.Sleep(5000); myImage.BackgroundImage = Properties.Resources.myImage; }In my c# app I am trying to change a button's background image and change it back after a few seconds. However, the background image is not changed until after the timer is up and then is instantly changed back to its original image before you can see the change.
private void button1_Click(object sender, EventArgs e) { myImage.BackgroundImage = Properties.Resources.newImage; System.Threading.Thread.Sleep(5000); myImage.BackgroundImage = Properties.Resources.myImage; }最满意答案
假设这是一个WPF应用程序,请添加DispatcherTimer ,如以下代码段所示:
DispatcherTimer _dispatcherTimer = new DispatcherTimer(); public MainWindow() { InitializeComponent(); button1.Click += button1_Click; _dispatcherTimer.Tick += new EventHandler(dt_Tick); _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 5); } private void button1_Click(object sender, RoutedEventArgs e) { myImage.BackgroundImage = Properties.Resources.newImage; _dispatcherTimer.Start(); } void dt_Tick(object sender, EventArgs e) { _dispatcherTimer.Stop(); myImage.BackgroundImage = Properties.Resources.myImage; }希望这会有所帮助。
Assuming this is a WPF app, add the DispatcherTimer as shown in the following code snippet:
DispatcherTimer _dispatcherTimer = new DispatcherTimer(); public MainWindow() { InitializeComponent(); button1.Click += button1_Click; _dispatcherTimer.Tick += new EventHandler(dt_Tick); _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 5); } private void button1_Click(object sender, RoutedEventArgs e) { myImage.BackgroundImage = Properties.Resources.newImage; _dispatcherTimer.Start(); } void dt_Tick(object sender, EventArgs e) { _dispatcherTimer.Stop(); myImage.BackgroundImage = Properties.Resources.myImage; }Hope this will help.
更多推荐
更改,图像,change,myImage,image,电脑培训,计算机培训,IT培训"/> <meta name="de
发布评论