您的位置:首页 > 其它

WPF ProgressBar显示进度(一)

2014-06-25 11:11 381 查看
ProgressBar显示进度的做法一般采用线程来实现,譬如

xaml代码:

<Window x:Class="MVVM3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock x:Name="tb" Height="23"></TextBlock>
<ProgressBar x:Name="pb" Minimum="0" Maximum="100" Height="20" ></ProgressBar>
<Button Content="click" Width="72" Height="23" Click="Button_Click_1"></Button>
</StackPanel>
</Window>


cs代码:

namespace MVVM3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(new ThreadStart(() =>
{
for (int i = 1; i <= 100; i++)
{
this.pb.Dispatcher.Invoke(() => this.pb.Value = i);
this.tb.Dispatcher.Invoke(() => this.tb.Text = i + "%");
Thread.Sleep(200);
}
}));
thread.Start();
}
}
}
点击click按钮就能看到进度条的进度显示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: