MVVM关闭文件的方式可以取消 [英] MVVM way to close document with possibility to cancel out

查看:136
本文介绍了MVVM关闭文件的方式可以取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Avalondock 2.x for one of my open source projects, if a document is dirty when you close it you should be able to cancel the close.

我使用的是Caliburn Micro和Coroutine,我唯一能够解决的方法就是使用CM附加到活动中

I am using Caliburn Micro and Coroutine, only way I have been able to solve it is to use C.M to attach to the event

<i:EventTrigger EventName="DocumentClosing">
    <cal:ActionMessage MethodName="DocumentClosing">
        <cal:Parameter Value="$documentcontext" />
        <cal:Parameter Value="$eventArgs" />
    </cal:ActionMessage>
</i:EventTrigger>

事件arg具有cancel属性。这个方法的问题在于,它对MVVM不太友好,我创建了一些辅助方法来对此进行Coroutinify,例如

The event arg has a cancel property. Problem with this approuch is thats its not very MVVM friendly, I have created a little helper method to Coroutinify this like

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

            return r;
        });
}

用法类似

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

此方法有效,但是有点笨拙,是否还有更多的MVVM方法关闭具有取消功能的Avalondock中的文档?

This works but it's a bit clumsy etc, is there a more MVVM way of closing documents in Avalondock with cancel ability?

编辑:源代码

< a href = https://github.com/AndersMalmgren/FreePIE/blob/master/FreePIE.GUI/Shells/MainShellView.xaml#L29 rel = nofollow> 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按钮,右键单击关闭 /全部关闭)。然后,如果需要,您将负责删除(关闭)文档。

The way I've accomplished this is by binding to the CloseCommand property of an AvalonDock LayoutItem. When this binding is associated, it overrides the default behavior of closing a document ('X' button, right click close / close all). You then are fully responsible for removing (closing) the document if desired.

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

The way I set it up was to have a DocumentManagerVM which contains an ObservableCollection of DocumentVMs. Each DocumentVM has an ICommand called RequestCloseCommand, which can close the document by removing itself from the collection of DocumentVMs it's owning DocumentManagerVM.

特别是在我的DocumentVM视图模型中,有一个ICommand (我正在使用mvvmLight RelayCommand)执行关闭逻辑:

Specifically, in my DocumentVM viewmodel, there's an ICommand (I'm using mvvmLight RelayCommand) to perform the closing logic:

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中设置绑定。

In your view, set up your binding in the LayoutItemContainerStyle or LayoutItemContainerStyleSelector.

<ad:DockingManager
    DataContext="{Binding DocumentManagerVM}"
    DocumentsSource="{Binding DocumentVMs}">

    <ad:DockingManager.LayoutItemContainerStyle>
        <Style TargetType="{x:Type ad:LayoutItem}">
            <Setter Property="Title" Value="{Binding Model.Header}"/>
            <Setter Property="CloseCommand" Value="{Binding Model.RequestCloseCommand}"/>
        </Style>
    </ad:DockingManager.LayoutItemContainerStyle>

</ad:DockingManager>

这篇关于MVVM关闭文件的方式可以取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆