我在WPF中使用向导组件。 单击向导的Finish按钮时,我会执行一些文件操作,大约需要5秒钟。
我想在文件操作期间显示进度条。
所以,我做了:
Thread t = new Thread(new ThreadStart(() =>
{
MessageWithProgressBar progress = new MessageWithProgressBar(this);
progress.Show();
}));
t.SetApartmentState(ApartmentState.STA);
t.Start();
来自MessageWithProgressBar.xaml的一些代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Name="title" Content="Please wait while processing.." FontSize="17.333"/>
</Grid>
<Grid Grid.Row="1" Margin="0,0,0,20">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ProgressBar Name="progress" IsIndeterminate="True" Value="{Binding CurrentStep, Converter={StaticResource stepToProgress}}" />
<TextBlock Name="progressLabel" Text="{Binding ElementName=progress, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Grid>
来自MessageWithProgressBar.xaml.cs的一些代码
public partial class MessageWithProgressBar : Window
{
public MessageWithProgressBar(MainWindow Mw)
{
InitializeComponent();
this.DataContext = Mw;
}
}
public class currentStepToProgressValue : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is int)
{
if ((int)value <= GeneralProperties.General.thresholdStep)
return (int)Math.Floor((decimal)(8 * (int)value));
}
return 0;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我的问题是进度条立即消失,我看不到进度条。
有什么建议吗?