2
votes

I have a problem with Caliburn.Micro: I have ShellView.xaml and ShellViewModel.cs and I want to open new dialog window 'NewDialogView.xaml' from the ShellViewModel.

 <StackPanel>
     <Button x:Name="Click"
             Content="Click"
             />
 </StackPanel>

 internal class ShellViewModel
 {
     public void Click()
     {
         Window newDialogView = new NewDialogView();
         newDialogView.Show();
     }
 }

Then when a user is in that new window, he/she can click on the button and get some message:

 <StackPanel>
     <Button x:Name="ShowMessage"
             Content="Click"
             />
 </StackPanel>

 internal class NewDialogViewModel
 {
     public void ShowMessage()
     {
         MessageBox.Show("Hello!");
     }
 }

The problem is that when a user clicks on the button in NewDialogView.xaml nothing happens. There is no messagebox with the content 'Hello'. Please help!

2
Reference WindowManagermvermef
I figured it out. Thanks.Navuhodonosor

2 Answers

7
votes

Just wanted to share the simplest way i could possibly make on how to open new window:

IWindowManager manager = new WindowManager();

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

And how to close it...

3
votes

Just to expand on what @mvermef said in a comment above.

You need to use Caliburn Micro's window manager to show your dialog. Caliburn will then be able to do all of the plumbing in the background. Typically you would use the Caliburn bootstrapper to create the ShellViewModel with the window manager.

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...
    }
}