无需为您完成整个过程,您可以创建一个在活动窗口顶部打开并带有进度条的窗口,然后通过事件将进度发送给它。
我刚刚创建了这个进度条:
<Window x:Class="ProgressBar.Views.ProgressBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
WindowStyle="None"
Title="ProgressBar" Height="25" Width="250">
<Grid VerticalAlignment="Center">
<ProgressBar Minimum="0" Maximum="100" Height="25" Value="{Binding Progress}"></ProgressBar>
</Grid>
视图模型看起来像:
public class ProgressViewModel:INotifyPropertyChanged
{
private int _progress;
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ProgressViewModel()
{
ProgressBarEvent.GetInstance().ProgressChanged += (s, e) => Application.Current.Dispatcher.Invoke(()=> Progress = e);
}
public int Progress
{
get => _progress;
set
{
_progress = value;
OnPropertyChanged();
}
}
}
事件看起来像
public class ProgressBarEvent
{
private static ProgressBarEvent _instance;
public static ProgressBarEvent GetInstance()
{
if(_instance == null)
_instance = new ProgressBarEvent();
return _instance;
}
public EventHandler<int> ProgressChanged;
public EventHandler Show;
public EventHandler Close;
public ProgressBarEvent Start()
{
OnShow();
return this;
}
public ProgressBarEvent Stop()
{
OnStop();
return this;
}
private void OnShow()
{
if (Show is EventHandler show)
{
show.Invoke(this, new EventArgs());
}
}
private void OnStop()
{
if (Close is EventHandler close)
{
close.Invoke(this, new EventArgs());
}
}
public void SendProgress(int progress)
{
if (ProgressChanged is EventHandler<int> progressChanged)
{
progressChanged.Invoke(this, progress);
}
}
}
App.xaml.cs 看起来像:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
ProgressBarEvent.GetInstance().Show += ShowProgressBar;
ProgressBarEvent.GetInstance().Close += CloseProgressBar;
}
private Views.ProgressBar progressBarWindow;
private void CloseProgressBar(object sender, EventArgs e)
{
Application.Current.Dispatcher.Invoke(() => progressBarWindow.Close());
}
private void ShowProgressBar(object sender, EventArgs e)
{
progressBarWindow = new Views.ProgressBar();
var activeWindow = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
progressBarWindow.Owner = activeWindow;
progressBarWindow.Show();
}
}
然后我在 MainWindow 上创建了一个按钮,该按钮具有如下所示的事件处理程序:
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
ProgressBarEvent.GetInstance().Start();
await Task.Run(() =>
{
for (int i = 0; i < 100; i++)
{
ProgressBarEvent.GetInstance().SendProgress(i);
Thread.Sleep(100);
}
ProgressBarEvent.GetInstance().Stop();
});
}
基本上,这将打开进度条窗口并在完成后将其关闭。您必须弄清楚如何将进度放置在要运行进度的窗口上。