public void OpenFile(string path) { var CsfClassFileBW = new Data.CsfBackgroundWorker <Kernel.CsfClassFile>(); FilePath = path; var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); IsOpenedFile = false; void finished(Data.CsfBackgroundWorker <Kernel.CsfClassFile> sender, Kernel.CsfClassFile result) { CsfClassFileBW.Finished -= finished; CsfClassFileBW.PropertyChanged -= propChanged; StatusBlock.Button.Click -= btnAct; DataContext = new ViewModel.CsfDocument(result); IsOpenedFile = true; if (result != null) { StatusBlock.Hide(); } }; void btnAct(object o, RoutedEventArgs e) { CsfClassFileBW.Finished -= finished; CsfClassFileBW.PropertyChanged -= propChanged; StatusBlock.Button.Click -= btnAct; CsfClassFileBW.Cancel(); StatusBlock.TextBlock.Text = "用户已取消"; StatusBlock.HideProgressBar(); StatusBlock.HideButton(); fs.Close(); } void propChanged(object o, System.ComponentModel.PropertyChangedEventArgs e) { switch (e.PropertyName) { case nameof(CsfClassFileBW.StatusString): // 字符串变更 lock (StatusBlock.TextBlock) StatusBlock.TextBlock.Text = CsfClassFileBW.StatusString; break; case nameof(CsfClassFileBW.UnknowProgress): // 进度未知 if (CsfClassFileBW.UnknowProgress) { StatusBlock.ProgressBar.IsIndeterminate = true; } else // 已知 { StatusBlock.ProgressBar.IsIndeterminate = false; StatusBlock.ShowProgressBar(); StatusBlock.ProgressBar.Maximum = CsfClassFileBW.MaxProgress; } break; case nameof(CsfClassFileBW.Progress): // 进度变更 lock (StatusBlock.ProgressBar) StatusBlock.ProgressBar.Value = CsfClassFileBW.Progress; break; case nameof(CsfClassFileBW.Error): // 发生异常 if (CsfClassFileBW.Error is Exception exception) { CsfClassFileBW.Finished -= finished; CsfClassFileBW.PropertyChanged -= propChanged; StatusBlock.Button.Click -= btnAct; lock (StatusBlock.TextBlock) StatusBlock.TextBlock.Text = exception.Message; StatusBlock.HideProgressBar(); StatusBlock.HideButton(); fs.Close(); } break; } } // 显示状态块 StatusBlock.Show(StatusBlock.Control.ProgressBar | StatusBlock.Control.TextBlock); StatusBlock.ProgressBar.IsIndeterminate = true; StatusBlock.TextBlock.Text = "正在读取中, 请稍候..."; StatusBlock.Button.Content = "取消"; StatusBlock.ShowButton(); StatusBlock.Button.Click += btnAct; // 属性变更通知 CsfClassFileBW.PropertyChanged += propChanged; // 任务完成 CsfClassFileBW.Finished += finished; // 加载文件 CsfClassFileBW.BeginParse(fs); }