我有問題更新從另一個班級內的後臺工作人員單獨打開的窗口的進度欄。單獨的視圖不從後臺工作人員Mvvm
程序執行是這樣的:
- 主窗口負荷
- 點擊按鈕做了一些工作,並打開一個彈出 進度條(新打開的窗口)
- 背景工人做工作 和報告進度彈出進度條
- 彈出進度條 希望更新。
進度條值被綁定到一個屬性,在step-through調試器中,該屬性看起來被後臺工作人員更新好了。這些更改不會反映在彈出的進度欄視圖中。但是,綁定不會中斷,因爲如果我手動嘗試並設置進度欄的屬性值,它可以正常工作。此外,當我將進度條放入最初啓動的MainWindow視圖中時,它會更新正常。有什麼建議麼??
下面是一些代碼:
MainWindowViewModel
public class MainWindowViewModel: BaseViewModel
{
private void PerformSomeAction()
{
var popUpProgressBar = new PopUpProgressBarViewModel();
popUpProgressBar.Show(popUpProgressBar);
var worker = new BackgroundWorker { WorkerReportsProgress = true };
worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
if (args.ProgressPercentage != popUpProgressBar.Progresser)
{
Progresser = args.ProgressPercentage;
popUpProgressBar.Progresser = args.ProgressPercentage;
}
};
worker.DoWork += delegate
{
for (int i = 0; i < 101; i++)
{
worker.ReportProgress(i);
System.Threading.Thread.Sleep(10);
}
MessageBox.Show("Done");
};
worker.RunWorkerAsync();
}
private int _progresser;
public int Progresser
{
get { return _progresser; }
set
{
if (_progresser == value) return;
_progresser = value;
OnPropertyChanged("Progresser");
}
}
private RelayCommand _startProcessing; //set private member
public ICommand StartProcessing //public field used by xaml binding
{
get
{
return _startProcessing = MakeCommandSafely(_startProcessing,() => PerformSomeAction());
}
}
}
PopUpProgressBarViewModel
public class PopUpProgressBarViewModel : BaseViewModel
{
private PopUpProgressBar _popUpProgressBar;
public void Show(PopUpProgressBarViewModel context)
{
_popUpProgressBar = new PopUpProgressBar {DataContext = context};
_popUpProgressBar.Show();
}
private int _progresser;
public int Progresser
{
get { return _progresser; }
set
{
if (_progresser == value) return;
_progresser = value;
OnPropertyChanged("Progresser");
}
}
}
對於完整的解決方案的文件(這樣你可以看到發生了什麼),see here
哈哈該死的,剛纔那一條線。謝謝堆:) – Lukehb 2012-04-20 00:51:10