rskibbe.UI.Screens.Wpf – A common ground for WPF MVVM related UI infrastructure
Inhaltsverzeichnis
What is rskibbe.UI.Screens.Wpf?
This is a sub package for the base NuGet package extending the infrastructure provided by „rskibbe.UI.Screens„. This package is dependent on CommunityToolkit.MVVM package, as it’s the most commonly used one in terms of WPF and MVVM. I decided to not stripe it away, as I think, that this is the absolute core of using MVVM – therefore I’m currently not planning to remove it in the near future.
Currently, this package is also dependent on the Autofac „Dependency Injection Library“ as I’m using it in almost every app I build. I spent some time making it based on the „IServiceProvider“ interface, but as it’s not as powerful, I decided to drop it – especially in terms of my own time. Maybe I’ll change that in the future, but currently, this isn’t planned as well.
Keep these two things in mind, when using the package!
The documentation is still a work in progress. Please understand that I do this for free and in my free time.
Installation
Use the NuGet Package Manager of Visual Studio to install the base package (and corresponding sub-packages if wanted/needed). The easiest way to access the Manager, is by clicking the „Extra“ menu tool strip menu item, or by right-clicking your project inside the project explorer. Then click something like „Manage NuGet Packages“. There you should be easily able to search for „rskibbe.UI.Screens.Wpf“ and see all possible packages.
Or you can use the following NuGet Package Manager Console commands for the newest versions:
Install-Package rskibbe.UI.Screens Install-Package rskibbe.UI.Screens.Wpf
Key Takeaways
WpfApp Class
In general, the „System.Windows.Application“ class is the core of each WPF application. For my use cases I mostly have recurring things I have to do, like:
- Registering Dependencies
- Defining the start window
- Instantiating the starting viewmodel
- Overriding „OnStartup“ method
- etc…
To make these steps easier I created this class which combines / wraps these „every time I start a new WPF app“ steps. Let your own App derive from the WpfApp class and override the abstract methods to kickstart everything.
There are some conventions going on behind the scenes, to avoid overhead, repetitive and annoying duplications:
RegisterViewModels
This method gets called automatically behind the scenes to register the ViewModels of your application. Take a look at it’s description to learn more about finetuning these aspects.
Primarily there are 3 important methods to be overriden:
RegisterAdditionalDependencies
Use this method to like register services, repositories and anything else.
GetMainWindowType
This method should return the type of the MainWindow, as my app can’t know which Window-Class you’re going to use for your individual project. The class needs to have an empty constructor to be instantiated.
GetMainWindowViewModelType
This method should return the type of the MainViewModel class, as my app can’t know which ViewModel class you’re going to use for your individual project.
WpfWindow Class
The WpfWindow class represents the MainWindow of the app. It implements „IWpfWindow“ from above, is therefore having a „CurrentScreen“ implementing „IScreen“. It inherits the typical „System.Windows.Window“ WPF class. In modern app development, it’s usual to have some sort of main window which is just switching between different sub-screens.
Usually, you should use a derived class of the „HostScreenViewModelBase“ if you want that navigation-ish app style, or you could use derived class of the „ScreenViewModelBase“ if you want like a one-screen app.
ViewModelBase Class
Nothing big to be seen here, the „ViewModelBase“ class is just – as it’s suggesting – a base class for any kind of ViewModel. It depends on the „CommunityToolkit.MVVM“ ObservableObject class.
ScreenViewModelBase Class
<more to come>
Future plans / Todos
Here are some future plans and potential tasks I hope to tackle in the near future. However, please keep in mind that this project is a passion project done in my free time, so I can’t guarantee if or when these updates will be completed.
- More documentation
- Getting rid of unnecessary dependencies
- Logging without Debugger (something like Microsoft.Extensions.Logging.ILogger)