我有一个使用 Galasoft MVVM Toolkit 以 WPF 作为客户端的多程序集项目。我想知道如何将 ProgressControl 的“Value”属性数据绑定到 BackgroundWorker 的 ProgressChanged 事件。
还有其他文章处理这种情况;然而,它们只处理位于虚拟机中的 Worker。我的 Worker 理想地位于 DataService 存储库中,它完成了大部分繁重的工作,如下面的代码所示。
这是我得到的:
在视图中,ProgressControl XAML 是这样的:
<ProgressBar Maximum="{Binding NumberOfPublications}"
Minimum="0"
Height="50"
Margin="10"
Value="{Binding Source={StaticResource ConverterEntity},Path=ProgressValue, Mode=OneWay}"
Visibility="{Binding Source={StaticResource ConverterEntity}, Path=IsLengthyOperation, Converter={StaticResource Bool2InverseVisibility}}">
绑定源位于另一个程序集中,如下所示:
public class ConverterEntity : ViewModelBase
{
.
.
public int ProgressValue
{
get
{
return _progressValue;
}
set
{
_progressValue = value;
RaisePropertyChanged( ProgressValuePropertyChanged );
}
}
回到 App.xaml,我已将必要的引用放入 Windows 资源中,如下所示:
<Application.Resources>
<vc:ConverterEntity x:Key="ConverterEntity" />
显然,我可以看到虽然 View xaml 构建得很好,但没有使用“vc”引用...只是想知道它如何正确构建,即使 ProgressBar 不需要“vc”命名法。
无论如何,ViewModel 有一个按钮绑定到运行此代码的命令:
repo.ExtractAllZipArchiveSets(
( s , e ) =>
... );
...这是一个执行 grunt 代码的存储库:
public void ExtractAllZipArchiveSets( Action<ObservableCollection<Publication>
, Exception> callback , string rootDirectory ) {
.
.
DirectoryInfo dirInfo = new DirectoryInfo( rootDirectory );
BackgroundWorker worker = null;
Thread.Sleep( 2000 );
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler( ExtractZipFile );
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( AddPublicationToBeProcessed );
worker.ProgressChanged += ProgressChanged;
worker.RunWorkerAsync( zipInfo );
worker.WorkerReportsProgress = true;
worker.ReportProgress( (dirCounter +=1 ) * 10);
.
.
}
public void ProgressChanged( object sender , ProgressChangedEventArgs e )
{
//ConverterEntity entity = new ConverterEntity();
//entity.ProgressValue = e.ProgressPercentage;
}
private void AddPublicationToBeProcessed(object sender, RunWorkerCompletedEventArgs e)
{
}
private void ExtractZipFile( object sender , DoWorkEventArgs e )
{
FileInfo fileInfo = new FileInfo( ( e.Argument as FileSystemInfo ).FullName );
DiskFolder destinationFolder = new DiskFolder( fileInfo.Directory.ToString() );
DiskFile zipFile = new DiskFile( fileInfo.FullName );
ZipArchive zip = new ZipArchive( zipFile );
zip.CopyFilesTo( destinationFolder , true , true , "*.r*" );
}
...就是这样。
我不想在虚拟机中使用我的繁琐代码,因为我只想从中调用并执行操作方法。存储库似乎是抽象所有这些工作的适当位置。
也许某种 DependencyObject 可以解决这个问题,而不是像我这样使用实例化和私有字段,这显然是行不通的。
但是,如果我想为 ProgressControl 提供更新,我该怎么做?
谢谢。