我在我的一个开源项目中使用了Avalonvok2.x,如果文档在关闭时是脏的,那么应该可以取消关闭。
我用的是卡利伯恩公司和Coroutine,我唯一能解决的办法就是用C.M来附加到事件中
<i:EventTrigger EventName="DocumentClosing">
<cal:ActionMessage MethodName="DocumentClosing">
<cal:Parameter Value="$documentcontext" />
<cal:Parameter Value="$eventArgs" />
</cal:ActionMessage>
</i:EventTrigger>event具有cancel属性。这个附加程序的问题是它不是很友好的MVVM,我创建了一个小助手方法来实现这个
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方式关闭文件在Avalon多克与取消能力?
编辑:源代码
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
发布于 2013-10-09 15:51:23
实现这一点的方法是绑定到CloseCommand属性的AvalonDock LayoutItem。当此绑定相关联时,它将重写关闭文档的默认行为(“X”按钮,右键单击“关闭/关闭所有”)。然后,如果需要,您将完全负责移除(关闭)文档。
我设置它的方式是有一个DocumentManagerVM,其中包含DocumentVM的ObservableCollection。每个DocumentVM都有一个名为RequestCloseCommand的ICommand,它可以通过从它所拥有的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中设置绑定。
<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>发布于 2021-10-11 10:54:45
我向DockingManger添加了一个依赖项属性,该属性允许绑定到关闭命令:
public static class DocumentClosingBehavior
{
#region Dependecy Property
private static readonly DependencyProperty DocumentClosingCommandProperty = DependencyProperty.RegisterAttached
(
"DocumentClosingCommand",
typeof(ICommand),
typeof(DocumentClosingBehavior),
new PropertyMetadata(DocumentClosingCommandPropertyChangedCallBack)
);
#endregion
#region Methods
public static void SetDocumentClosingCommand(this UIElement inUIElement, ICommand inCommand)
{
inUIElement.SetValue(DocumentClosingCommandProperty, inCommand);
}
private static ICommand GetDocumentClosingCommand(UIElement inUIElement)
{
return (ICommand)inUIElement.GetValue(DocumentClosingCommandProperty);
}
#endregion
#region CallBack Method
private static void DocumentClosingCommandPropertyChangedCallBack(DependencyObject inDependencyObject, DependencyPropertyChangedEventArgs inEventArgs)
{
DockingManager uiElement = inDependencyObject as DockingManager;
if (null == uiElement) return;
uiElement.DocumentClosing += (sender, args) =>
{
GetDocumentClosingCommand(uiElement).Execute(args);
};
}
#endregion
}在XAML中:
<xcad:DockingManager vm:DocumentClosingBehavior.DocumentClosingCommand="{Binding DocumentCloseCommand}" Grid.Row="2"
AllowMixedOrientation="True"
BorderBrush="Black"
BorderThickness="1"
Theme="{Binding ElementName=_themeCombo, Path=SelectedItem.Tag}"
DocumentsSource="{Binding Documents}"
ActiveContent="{Binding ActiveDocument, Mode=TwoWay, Converter={StaticResource ActiveDocumentConverter}}"
>在我的MainViewModel中,我定义了一个ICommand DocumentCloseCommand。
https://stackoverflow.com/questions/19268197
复制相似问题