Mahhaps-库类的“显示对话框”

时间:2018-08-24 20:30:56

标签: c# wpf mahapps.metro

我在ViewModel上使用IDialogCoordinator,一切正常。

但是如果在viewmodel中我有其他类的实例,则它不起作用。我尝试在构造函数中传递dialogCoordinator,但是我收到一条消息“上下文未注册”。

如何实现?

Viewmodel:

public viewModel
{
  private IDialogCoordinator dialogCoordinator;
  public ICommand SyncCommand { get { return new RelayCommand(SyncCommand_ExecutedAsync); } }  

  public viewModel(IDialogCoordinator _coordinator)
  {
    coordinator = _coordinator;
  }

  private void SyncCommand_ExecutedAsync()
  {
        var sync = new IndexUpdater(dialogCoordinator);
        var b = sync.Start().Result;
  }
}

IndexUpdater:

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

public async Task<bool> Start(bool reset = false)
    {
        var output = true;

        ProgressDialogController controller = await dialogCoordinator.ShowProgressAsync(this, "HEADER", "MESSAGE", true, new MetroDialogSettings { NegativeButtonText = "Close" });
         controller.Minimum = 0;
        controller.Maximum = 100;

        for (int x = 0; x < 100; x++)
        {
            if (controller.IsCanceled)
            {
                output = false;
                break;
            }

            Thread.Sleep(200);

            controller.SetProgress(x + 1);
            controller.SetMessage("x = " + x);
        }

        await controller.CloseAsync();
        return output;
}

在这种情况下,我遇到异常: “ InvalidOperationException:未注册上下文。请考虑使用DialogParticipation.Register在XAML中绑定到DataContext中。”

如果将ProgressDialogController直接放在viewModel.SyncCommand_ExecutedAsync()方法中,那么它将起作用。

如何注册IndexUpdater类或如何在其中传递dialogCoordinator?

预先感谢

0 个答案:

没有答案