caliburn.micro:从主窗口打开新窗口,然后将其连接到ViewModel



我有caliburn.micro的问题:我有shellView.xaml和shellViewModel.cs,我想打开新的对话框窗口'newdialogview.xaml'shellViewModel。

 <StackPanel>
     <Button x:Name="Click"
             Content="Click"
             />
 </StackPanel>
 internal class ShellViewModel
 {
     public void Click()
     {
         Window newDialogView = new NewDialogView();
         newDialogView.Show();
     }
 }

然后,当用户在该新窗口中时,他/她可以单击按钮并获取一些消息:

 <StackPanel>
     <Button x:Name="ShowMessage"
             Content="Click"
             />
 </StackPanel>
 internal class NewDialogViewModel
 {
     public void ShowMessage()
     {
         MessageBox.Show("Hello!");
     }
 }

问题是,当用户单击newdialogview.xaml中的按钮时,什么也不会发生。没有内容框,其中包含内容" Hello"。请帮助!

只是想分享我可以在如何打开新窗口的方式上可以做出的最简单方法:

IWindowManager manager = new WindowManager();
public void Button()
{
    manager.ShowWindow(new MyViewModel(), null, null);
}

以及如何关闭 ...

只是为了扩展@mvermef在上面的评论中所说的话。

您需要使用Caliburn Micro的窗口管理器来显示您的对话框。然后,卡利本将能够在后台完成所有管道。通常,您会使用Caliburn Bootstrapper使用窗口管理器创建ShellViewModel

internal class ShellViewModel
{
    public ShellViewModel(IWindowManager theWindowManager)
    {
        this.windowManager = theWindowManager;
    }
    public void Click()
    {
        // Assumes that there is a NewDialogView...
        var newDialogViewModel = new NewDialogViewModel();
        bool? result = this.windowManager.ShowDialog(newDialogViewModel);
        // result is true if user pressed ok button...
    }
}

相关内容

最新更新