Closing Event和Catel的MessageService,无法取消

时间:2015-06-30 15:56:03

标签: c# wpf catel

我正在使用ExitCommand从Menu->退出关闭应用程序,我试图在用户点击X时实现相同的退出行为

我已经在我的XAML

中定义了这个
<catel:Window x:Class="IF.Tesoreria.Client.WPF.Views.ShellView"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:catel="http://catel.codeplex.com"
          xmlns:view="clr-namespace:IF.Tesoreria.Client.WPF.Views"
          xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
          xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
          Icon="../Media/threasure.gif" Height="500" Width="800" >
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Closing">
        <catel:EventToCommand Command="{Binding ExitCommand}"  PassEventArgsToCommand="True"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<!--omiss-->

在ViewModel中我使用了以下命令

   public Command<CancelEventArgs> ExitCommand { get; private set; }

   #region CTor
    public ShellViewModel( IDynamicContainer dynamicContainer, IViewModelFactory viewModelFactory)
    {
        this.dynamicContainer = dynamicContainer;
        this.viewModelFactory = viewModelFactory;

        ShowInfoCommand = new Command(ShowInfoCommandExecute);
        ExitCommand = new Command<CancelEventArgs>(OnExitCommandExecute);
    }
    #endregion

private void OnExitCommandExecute(CancelEventArgs args)
    {
        var res = MessageBox.Show(Properties.Resources.DIALOG_EXIT, Properties.Resources.DIALOG_EXIT_CAPTION,MessageBoxButton.YesNo);
        //var res = messageService.Show(Properties.Resources.DIALOG_EXIT, Properties.Resources.DIALOG_EXIT_CAPTION,
        //      MessageButton.YesNo, MessageImage.Question);

        if (res == MessageBoxResult.Yes)
        {
            ICloseApplicationService closeApplicationService = this.GetServiceLocator().ResolveType<ICloseApplicationService>();
            closeApplicationService.Close();
        }
        else
        {
            args.Cancel = true;
        }
    }

如果我使用MessageBox,我可以取消关闭事件(设置args.Cancel = true)。否则,如果我使用IMessageService,当我从Show中得到结果时为时已晚

我做错了什么? 感谢

1 个答案:

答案 0 :(得分:0)

Catel中的IMessageService是异步的。我们正在考虑以不同的方式实现这一点。在此期间,使用INavigationService.ClosingApplication事件。