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
990 stars 143 forks source link

Crashes on loading main window in folder #118

Closed micah686 closed 4 years ago

micah686 commented 4 years ago

I'm not sure if this is the right place to ask this, but when I launch the program, it always seems to crash. Anyone have any idea what I'm doing wrong? image

Boostrapper.cs

using Stylet;
using Tester.ViewModel;
namespace Tester
{
    public class Boostrapper: Bootstrapper<MainViewModel>
    {
    }
}

MainView.xaml

<Window x:Class="Tester.Views.MainView"       
       <!--cut content, standard xmlns -->
        Title="MainView" Height="450" Width="800">
    <Grid>        
    </Grid>
</Window>

MainViewModel.xaml

using Stylet;
namespace Tester.ViewModel
{
    public class MainViewModel: Screen
    {
        private readonly WindowManager _windowManager;

        public MainViewModel(IWindowManager windowManager)
        {
            _windowManager = (WindowManager)windowManager;
        }
    }
}
canton7 commented 4 years ago

What exception does it throw?

Also, why cast the IWindowManager to WindowManager? Not that it should be causing a crash...

On 15 April 2020 06:09:04 BST, micah686 notifications@github.com wrote:

I'm not sure if this is the right place to ask this, but when I launch the program, it always seems to crash. Anyone have any idea what I'm doing wrong? image

Boostrapper.cs

using Stylet;
using Tester.ViewModel;
namespace Tester
{
   public class Boostrapper: Bootstrapper<MainViewModel>
   {
   }
}

MainView.xaml

<Window x:Class="Tester.Views.MainView"       
      <!--cut content, standard xmlns -->
       Title="MainView" Height="450" Width="800">
   <Grid>        
   </Grid>
</Window>

MainViewModel.xaml

using Stylet;
namespace Tester.ViewModel
{
   public class MainViewModel: Screen
   {
       private readonly WindowManager _windowManager;

       public MainViewModel(IWindowManager windowManager)
       {
           _windowManager = (WindowManager)windowManager;
       }
   }
}

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/canton7/Stylet/issues/118

micah686 commented 4 years ago

I get a Stylet.StyletViewLocationException: 'Unable to find a View with type Tester.View.MainView' And I was casting the window manager because I thought that was how I needed to do it, or should it just be IWindowManager? I've attached the project as well. StyletTester.zip

canton7 commented 4 years ago

Aha. As the message says, it's looking for Tester.View.MainView, but your MainView is called Tester.Views.MainView.

It works by replacing "ViewModel" with "View" in the type name and namespace. Your ViewModel is in "Tester.ViewModel" (singular), so it looks in "Tester.View" (singular). If you put the ViewModels in "Tester.ViewModels" (plural), it would look in "Tester.Views" (plural).

You can change the namespace that MainView (or MainViewModel) is in, or you can tell the ViewManager to look in Tester.Views, see NamespaceTransformations here.

Yes, the idea is that you use IViewManager. Then you can inject a mock ViewManager in your unit tests.

micah686 commented 4 years ago

So, a few questions. 1)I would have to keep both the View and ViewModel folders singular or plural, not one of each, right? 2) Do I have to keep all of the Windows/UserControls in the View(s) root folder, or can I have them in a Views/Dialogs/ folder, for instance? I was having crashing trouble when I was testing additional windows out before, though I think they could just be this main issue.

canton7 commented 4 years ago

1) Unless you set up your own name mappings, yes 2) You can have sub folders. The algorithm is described here. So the View for ViewModels/Dialogs/FooViewModel.cs goes at Views/Dialogs/FooView.xaml. Likewise Dialogs/ViewModels/Foo.cs and Dialogs/Views/Foo.xaml, or Dialogs/FooViewModel.cs and Dialogs/FooView.xaml. Simply take the name and namespace of the ViewModel and replace "ViewModel" with "View" to get the name and namesoace of the View.

micah686 commented 4 years ago

So, all of the Views (Window, UserControl, Page,...) xaml files must end in ...View.xaml, and all of the associated Viewmodels must end in ...ViewModel.cs? Is that correct? Because if I have a Views/Windows/Test.xaml and a ViewModels\Windows\TestViewModel.xaml, it breaks, but a Views/Windows/TestView.xaml and a ViewModels\Windows\TestViewModel.xaml, it works correctly.


On a seperate note, could you set up a gitter.im chat for your project( it links with github and your project), so people can get answers to user support questions without opening up a ticket each time? It makes it a lot easier if people have a a quick question they need answering that isn't worth opening up an issue ticket for.

canton7 commented 4 years ago

Yes, but this is configurable, see my first link. I strongly recommend reading the whole of that documentation page, as it answers many of the questions you've asked.

There's typically no need to use Page - navigation is done using other mechanisms in Stylet, see the docs.

On 16 April 2020 06:24:37 BST, micah686 notifications@github.com wrote:

So, all of the Views (Window, UserControl, Page,...) xaml files must end in ...View.xaml, and all of the associated Viewmodels must end in ...ViewModel.cs? Is that correct?

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/canton7/Stylet/issues/118#issuecomment-614422671