嗨,我创建了 4 个矩形并给它们每个颜色。我现在尝试在按下时使用一个按钮来不断地随机更改这 4 个框的颜色(产生某种闪烁效果)。制定了以下代码,但是当我按下按钮时程序卡住了。
阅读System.Windows.Threading.DispatcherTimer
在这种情况下会有用,但不确定如何使用它。寻找有关如何使用它的建议。谢谢你。
namespace testDisco
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Random r;
Rectangle[] rects;
SolidColorBrush blue;
SolidColorBrush red;
SolidColorBrush yellow;
SolidColorBrush green;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
r = new Random();
rects = new Rectangle[4];
blue = new SolidColorBrush(Color.FromArgb(255,0,232,255));
red = new SolidColorBrush(Color.FromArgb(129,56,56,255));
yellow = new SolidColorBrush(Color.FromArgb(0,232,255,255));
green = new SolidColorBrush(Color.FromArgb(176,207,176,255));
rects[0] = rect1;
rects[1] = rect2;
rects[2] = rect3;
rects[3] = rect4;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int randomNumber = r.Next(4);
System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
while (true)
{
rects[randomNumber].Fill = red;
}
}
}
}