【问题标题】:Messages dialog fired the ViewModel through the DialogCoordinator using MahApps.Metro and MVVM Light Toolkit消息对话框使用 MahApps.Metro 和 MVVM Light Toolkit 通过 DialogCoordinator 触发 ViewModel
【发布时间】:2016-02-24 16:42:16
【问题描述】:

我在使用“MVVM Light Toolkit”和“MahApps.Metro”的 WPF 项目中遇到问题。

我正在尝试使用“MahApps.Metro”提供的“DialogCoordinator”功能来触发我的 ViewModel 的对话消息。但是,当执行“ShowMessageAsync”方法时,什么也没有发生。整个设置是根据文档执行的,无法确定不工作的原因。

以下相关代码。

必需的 XAML 属性:

xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}"

ViewModelLocator 构建器注册 MainViewModel 使用的 DialogCoordinator:

static ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    if (ViewModelBase.IsInDesignModeStatic)
    {
        SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
    }
    else
    {
        SimpleIoc.Default.Register<IDataService, DataService>();
    }

    SimpleIoc.Default.Register<IDialogCoordinator, DialogCoordinator>();
    SimpleIoc.Default.Register<MainViewModel>();
}

生成器 MainViewModel:

public MainViewModel(IDialogCoordinator dialogCoordinator)
{
    _dialogCoordinator = dialogCoordinator;            
}

RelayCommand 负责触发消息:

public RelayCommand<CancelEventArgs> ClosingWindow
{
    get
    {
        return _closingWindow
            ?? (_closingWindow = new RelayCommand<CancelEventArgs>(ExecuteClosingWindow));
    }
}
private RelayCommand<CancelEventArgs> _closingWindow;
private async void ExecuteClosingWindow(CancelEventArgs e)
{
    if (!IsQuitConfirmation) return;

    var result = await _dialogCoordinator.ShowMessageAsync(this, "Teste", "Teste", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings
    {
        AffirmativeButtonText = "OK",
        NegativeButtonText = "CANCELAR",
        AnimateShow = true,
        AnimateHide = false
    });            

    if (result == MessageDialogResult.Negative)
        e.Cancel = true;
}

【问题讨论】:

  • 我不确定我是否理解这个问题。 ShowMessageAsync() 被调用但什么也没发生?所以也不例外,屏幕上什么都没有?
  • 对不起,我的英语不太好。但是,正是它。 ShowMessageAsync() 被调用,但没有发生任何事情。

标签: c# wpf xaml mvvm dialog


【解决方案1】:

我确定了原因。它是如此简单,以至于它很愚蠢。 这个 RelayCommand 在我的 MainWindow 的 Closing 事件中被拍摄。由于应用程序的执行性很强并且方法是异步的,因此事件在消息触发之前以并行操作结束。

不管怎样,谢谢你的关注!

【讨论】:

    猜你喜欢
    • 2012-01-26
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多