WPF画布如何实时刷新
我用WPF写了一个简单的程序,就是在主窗体上放了一个Canvas和一个Button。当单击Button的时候,在Canvas上重复一个动作10次:就是每1秒画一个圆,放上。但我发现动作画完以前Canvas一直处于空白,直到10秒画完了才显示出来图像出来。想实现的效果是每1秒钟画布都更新一下图片。代码如下:MainWindow.xaml
程序代码:<Window x:Class="CanvasTest.MainWindow"
xmlns="http://schemas."
xmlns:x="http://schemas."
Title="MainWindow" Height="350" Width="525">
<Grid>
<Canvas Height="232" HorizontalAlignment="Left" Margin="12,12,0,0" Name="canvas1" VerticalAlignment="Top" Width="467"></Canvas>
<Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" Margin="12,265,416,23" />
</Grid>
</Window>MainWindow.xaml.cs
程序代码:public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
Ellipse el = new Ellipse();
el.Height = 30;
el.Width = 60;
el.Fill = Brushes.Red;
el.Stroke = Brushes.Black;
canvas1.Children.Add(el);
}
else
{
Ellipse el = new Ellipse();
el.Height = 30;
el.Width = 60;
el.Fill = Brushes.Yellow;
el.Stroke = Brushes.Green;
canvas1.Children.Add(el);
}
System.Threading.Thread.Sleep(1000);
}
}






