在WPF应用程序中更改Button的BackgroundImage(Changing Button's BackgroundImage in WPF app)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
在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

本文发布于:2023-04-13 12:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/38c5fe825f37f81e7981b0bec9ca10e7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Button   WPF   app   Changing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!