canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
988 stars 143 forks source link

IWindowManager.ShowWindow throws NullReferenceException #134

Closed jsimunac closed 4 years ago

jsimunac commented 4 years ago

I'm attempting my first project using Stylet. I created my initial view. The user enters information and retrieves information from the database. This all works fine - I can trace and see all the models being populated. When I try to open the second view/viewmodel using IWindowManager.ShowWindow I get a NullReferenceException. I'm following the documentation exactly to open the next window. When I instantiate the new viewmodel, it runs InitializeCommand and all models for the second viewmodel are populated as well. But the ShowWindow doesn't work. Could you please give me some advice, or an example? Thanks!

canton7 commented 4 years ago

Do you have anything on OnActivate / OnInitialActivate?

Can you create a minimal reproducible example which shows the problem, and post it?

jsimunac commented 4 years ago

Nothing in OnActivate / OnInitialActivate. I'll try to set up a small example tomorrow when I get in to work. Thanks!

jsimunac commented 4 years ago

Here is a small example. The first form has a button. Click it and it loads some data and calls the second viewmodel. The data from is loaded, but the window is not shown. Please let me know if you need anything else. Thanks for any help you can give.

ShowWindowTest.zip

canton7 commented 4 years ago

Your WindowManager is null:

image

If we look at the field declaration, we can see that it's never assigned to:

image

If we fix up the constructor to have an IWindowManager injected into it, and assign that to our field, things work:

public ShellViewModel(IWindowManager windowManager)
{
    WindowManager = windowManager;
}
jsimunac commented 4 years ago

Thank you. That did it. I actually had that in my original project, but had a lower case instead of upper case W when setting it, and that's why it was never assigned. I feel so dumb!