Getting started – Building your own WPF application using CaliburnMicro and AutoFac
First, I assume you are familiar with windows platform foundation (WFP), MVVM and .Net in general.
When working with MVVM, some of the simple control event or databinding become in some cases a tedious task. Instead, for example, creating a generic “Behavior” for a button control such as “Click”, you can simply create a method which can receive a “Sender” object information and “EventArgs” parameter just like code behind will do.
CaliburnMicro will provide you a more convenient way to work with your Application views – in another word, you’ll be able to put your time and efforts more in a logic part, less in how to combine view and ViewModel parts – you can read more about Caliburn Micro framework in the link below: Caliburn micro is a ViewModel first and it’s an important information that needs to be taken in account when starting up your own application.
https://caliburnmicro.com/documentation/introduction
Another tedious task, which is revealed as you progress with your application is class dependencies. They become more and more complicated. AutoFac gets the job done and makes Dependency Injection (DI) plain and simple (as it can be).
I’ll try to explain DI:
Let’s say we are having Class A which uses Class B – in order to create an instance of Class A, we have first to create an instance of Class B, but wait, what if Class B depends on Class D, and Class D, well… you got the point.
AutoFac allow you to register component (Classes in our Examples) and initialize them. You can dive in and learn more about AutoFac in the link bellow.
http://autofac.readthedocs.io/en/latest/getting-started/index.html
1. Start Creating Your Own WPF Application.
2. Add Caliburn.Micro and AutoFac to your main project by adding Nuget Packages.
3. Open App.Xaml and delete StartupUri
Doing so will remove main startup window (for now).
4. Create MVVM basic folder and Class, for that example create a ViewModels folder with MainWindowViewModel.cs and View folder with wpf window called MainWindowView.xmal
5. Create a bootstrapper class which inherit from bootstrapperbase (from Caliburn):
5.1. Create a Constant string with your app name – this will help Caliburn to “connect” multiple dll within your project.
5.2. Create a private IContainer which will hold links to your future dependencies components
5.3. Override Buildup Method
Each item that needs to work, must be register into Autofac Container!
5.4. Override Configure method and add your own registration logic :
5.4.1. Create ContainerBuilder
5.4.2. Create a private method that register your main window.
- I recommend to register all modules from each DLL via one file as i’ll show later.
- Try to separate your module registration to keep your application modularity:
**Types - which can be set as services, managers.
**Moudle – Can set most of viewmodels
5.4.3.Create Method Called RegisterType and add the following registration component (it a Must!)*
*it’s up to us to decide how each component can act, because we want only one instance of our main app, we will flag it as “single Instance”.
** it is highly recommended to create another file that map all components
5.5. Override OnStartup method and use it to show your main window
5.6. Override GetInstance and GetAllInstances
5.7. Create a new Method which search within given project folder all dlls and exes, this example below can be modified as you wish, please note that missing dll or exe can cause your application to partly work or none.
5.8. Override SelectAssemblies and use Method above
6. Open App.Xaml.cs under App.Xaml and Create an instance for BootStrapper and “initialize” it when app is starting.
7. We almost there…
7.1. Caliburn allow you do define each viewmodel as screen which is another word for view without window frame. A container which can hold, for example multiple screen.
7.2. In that example we define our main window as a conductor who can hold populate multiple screens but only one is active at once (.OneActive), if we wish to work with multiple screens without losing focus we will use .AllActive.
7.2.1.Open MainWindowViewModel.Cs and inherit the following class:
class MainWindowViewModel : Conductor<Screen>.Collection.OneActive
8. Start Your App an Empty Window appear, you can close it down, or stare at it ;).
9. Using DI.
9.1. Let’s Create a dll with the name: CaliburnAndAutoFac.ServiceProvider
9.1.1.Create a new folder with Name “Contracts” and add an Interface Call IHelloWorld which a signature “string GetInfo()”
9.1.2.Create a Class which implement IHelloWorld
9.1.3. Create a Module.cs file.
We actually want to register all our services in one place. NOTE! Each project must have reference to AutoFac, verify that you link the AutoFac nugget to your solution, not for a project.
9.1.4. Inherit from Autofac.Module and override Load Method.
9.1.5. It’s highly recommend to register each class using interface key, In this case we want a multiple instance, for that reason Module.cs class should Contain the following line:
9.1.6.Back to Bootstrapper do not forget to register the new module.
From that point you can use FirstHelloWorld service with AutoFac, I’ll show a quick example.
Open your main window view model and create a constructor that gets IHelloWorld
Create a String Property and get info from IHelloWorld.
Inside MainWindowView Use the magic of Caliburn and bind InfoText Into a TextBox, for example:
Run App and walla:
Have a great coding.
Hi Eldad! I tried your example, but see the error. Thank you, Natalia.