Caliburn.Micro:从主窗口打开新窗口,然后将其连接到viewmodel

问题描述 投票:0回答:2

我有一个Caliburn.Micro的问题:我有ShellView.xaml和ShellViewModel.cs,我想从ShellViewModel打开新的对话框窗口'NewDialogView.xaml'。

<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”的消息框。请帮忙!

wpf mvvm caliburn.micro caliburn
2个回答
2
投票

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

您需要使用Caliburn Micro的窗口管理器来显示您的对话框。然后Caliburn将能够在后台完成所有管道工作。通常,您将使用Caliburn引导程序使用窗口管理器创建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...
    }
}

2
投票

只是想分享一下我如何打开新窗口的最简单方法:

IWindowManager manager = new WindowManager();

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

And how to close it...

© www.soinside.com 2019 - 2024. All rights reserved.