MVVM关闭文件的方式有可能取消出来

我正在使用Avalondock 2.x作为我的一个开源项目,如果你关闭它时文档很脏,你应该可以取消关闭。

我正在使用Caliburn Micro和Coroutine,只有我能够解决它的方法是使用CM附加到事件

      

事件arg有取消属性。 这个approuch的问题是它不是很MVVM友好,我已经创建了一个小帮手方法来Coroutinify这个像

 public IEnumerable Coroutinify(IEnumerable results, System.Action cancelCallback) { return results.Select(r => { if (r is CancelResult) cancelCallback(); return r; }); } 

用过像

 public IEnumerable DocumentClosing(ScriptEditorViewModel document, DocumentClosingEventArgs e) { return Result.Coroutinify(HandleScriptClosing(document), () => e.Cancel = true); } 

这有效,但它有点笨拙等,是否有更多的MVVM方式关闭Avalondock的文件取消能力?

编辑:源代码

https://github.com/AndersMalmgren/FreePIE/blob/master/FreePIE.GUI/Shells/MainShellView.xaml#L29

https://github.com/AndersMalmgren/FreePIE/blob/master/FreePIE.GUI/Shells/MainShellViewModel.cs#L110

https://github.com/AndersMalmgren/FreePIE/blob/master/FreePIE.GUI/Result/ResultFactory.cs#L49

我完成此操作的方法是绑定到AvalonDock LayoutItem的CloseCommand属性。 当此绑定关联时,它会覆盖关闭文档的默认行为(“X”按钮,右键单击“关闭/关闭全部”)。 如果需要,您完全有责任删除(关闭)文档。

我设置它的方法是让DocumentManagerVM包含DocumentVM的ObservableCollection。 每个DocumentVM都有一个名为RequestCloseCommand的ICommand,它可以通过从拥有DocumentManagerVM的DocumentVM集合中删除自己来关闭文档。

具体来说,在我的DocumentVM视图模型中,有一个ICommand(我使用mvvmLight RelayCommand)来执行结束逻辑:

 public RelayCommand RequestCloseCommand { get; private set; } void RequestClose() { // if you want to prevent the document closing, just return from this function // otherwise, close it by removing it from the collection of DocumentVMs this.DocumentManagerVM.DocumentVMs.Remove(this); } 

在您的视图中,在LayoutItemContainerStyle或LayoutItemContainerStyleSelector中设置绑定。