La mise en œuvre de la barre de Progression de l'utilisation de WPF avec le pattern MVVM (à l'Aide de BackgroundWorker)
Remarque: Ce code fonctionne maintenant. J'ai corrigé une erreur stupide et j'ai aussi révisé de code que Steve Greatrex souligné.
Message Original LINK Comment mettre en place une barre de progression avec le pattern MVVM
ProgressbarSampleView.xaml
<UserControl x:Class="MyProject.ProgressbarSampleView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="718" Width="1024">
<ProgressBar Grid.Row="1"
Value="{Binding CurrentProgress, Mode=OneWay}"
Visibility="{Binding ProgressVisibility}"
Margin="22,0,25,0" />
<Button Grid.Row="2"
Content="Start Now"
Height="30"
Width="80"
HorizontalAlignment="Left"
Margin="22,4,0,0"
Name="btnStartNow"
VerticalAlignment="Top"
Command="{Binding Path=InstigateWorkCommand}"
/>
</UserControl>
ProggressbarSampleViewModel.cs
namespace MyProject
{
public class ProggressbarSampleViewModel: ViewModelBase
{
private readonly BackgroundWorker worker;
private readonly ICommand instigateWorkCommand;
public ProggressbarSampleViewModel()
{
this.instigateWorkCommand = new
RelayCommand(o => this.worker.RunWorkerAsync(), o => !this.worker.IsBusy);
this.worker = new BackgroundWorker();
this.worker.DoWork += this.DoWork;
this.worker.ProgressChanged += this.ProgressChanged;
}
public ICommand InstigateWorkCommand
{
get { return this.instigateWorkCommand; }
}
private int _currentProgress;
public int CurrentProgress
{
get { return this._currentProgress; }
private set
{
if (this._currentProgress != value)
{
this._currentProgress = value;
OnPropertyChanged("CurrentProgress");
}
}
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.CurrentProgress = e.ProgressPercentage;
}
private void DoWork(object sender, DoWorkEventArgs e)
{
//do time-consuming work here, calling ReportProgress as and when you can
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
_currentProgress = i;
OnPropertyChanged("CurrentProgress");
}
}
}
après avoir écrit une telle chaîne:" c'.instigateWorkCommand = new RelayCommand(o => ce.le travailleur.RunWorkerAsync, o => !c'.le travailleur.IsBusy);" . VS2010 renvoie une erreur: "délégué" du système.l'action "ne pas prendre 1 arguments". Pourriez-vous dire comment l'obtenir? Quelle est la version de .Net Framework utilisez-vous
OriginalL'auteur Shai | 2011-10-06
Vous devez vous connecter pour publier un commentaire.
La
StartNowCommand
jamais invoque leBackgroundWorker
- il juste de façon synchrone exécute laDoStartNow
méthode sur le thread d'INTERFACE utilisateur. Sur cette base, je suppose que lorsque vous cliquez sur le bouton lié à laStartNow
commande vous voyez votre INTERFACE utilisateur, le gel..?Vous devriez être en liaison votre bouton à la
InstigateWorkCommand
quiBackgroundWorker
code asynchrone.Dans cette mise en œuvre, je ne pense pas que vous aurez besoin de l'
StartNowCommand
à tous. Aussi, je ne vois pas leDoWork
gestionnaire d'événement n'importe où dans votre modèle de vue, donc je suis en supposant qu'il appelle justeDoStartNow
OriginalL'auteur Steve Greatrex