C# WPF MVVM development framework Caliburn Micro custom bootstrap ④

Posted by flowingwindrider on Thu, 13 Jan 2022 02:03:58 +0100

01 custom bootstrapper

In the previous section, we discussed Caliburn The most basic configuration of micro WPF application and demonstrates two simple functions related to operation and convention. In this section, I want to explore the Bootstrapper class further. Let's first configure the application to use the IoC container. In this case, we will use the built-in container, but Caliburn Micro can handle any container very well. First, continue with the code from Part 1. We will take this as our starting point. Now, let's create a new bootloader called SimpleBotstrapper. Use the following code:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;

public class SimpleBootstrapper : BootstrapperBase
{
    private SimpleContainer container;

    public SimpleBootstrapper()
    {
        Initialize();
    }

    protected override void Configure()
    {
        container = new SimpleContainer();

        container.Singleton<IWindowManager, WindowManager>();
        container.Singleton<IEventAggregator, EventAggregator>();

        container.PerRequest<ShellViewModel>();
    }

    protected override object GetInstance(Type service, string key)
    {
        return container.GetInstance(service, key);
    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return container.GetAllInstances(service);
    }

    protected override void BuildUp(object instance)
    {
        container.BuildUp(instance);
    }

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
        DisplayRootViewFor<ShellViewModel>();
    }

    protected override IEnumerable<Assembly> SelectAssemblies()
    {
        return new[] { Assembly.GetExecutingAssembly() };
    }
}

This is all the code to use the built-in container. First, we override the Configure method of the bootstrap class. This gives us the opportunity to set up the IoC container and perform any other framework configuration we may wish to perform, such as custom conventions. Here, we create a SimpleContainer and add WindowManager and EventAggregator, of course, ShellViewModel, but not ShellView, because we have assembly Source. Instance. So, what is assemblysouth Instance? This is Caliburn Where micro finds the view. You can add assemblies to this at any time during the application to make them available to the framework, but there is also a special location in the bootstrap to do so. Just override the SelectAssembly like this:

protected override IEnumerable<Assembly> SelectAssemblies()
{
    return new[] {
        Assembly.GetExecutingAssembly()
    };
}

All you have to do is return a list of searchable assemblies. By default, the base class returns the assembly where the application resides. So if all your views are in the same assembly as your application, you don't even have to worry about it. If you have multiple referenced assemblies that contain views, you need to remember that this is an extension point. In addition, if you are loading modules dynamically, you need to ensure that they are already in the IoC container and assemblysource. When loading modules Register in instance.

After I create the container and provide a directory for it, I make sure to add something specific to Caliburn Micro services. The framework provides the default implementation of IWindowManager and IEventAggregator. These are parts that I may rely on elsewhere, so I hope they can be used for injection. I also registered the container itself (just personal preference).

After configuring the container, we need to tell Caliburn How micro uses it. This is the purpose of the last three coverage. Framework requires' GetInstance 'and' getAllInstance '. Build can optionally be used to provide attribute dependencies to IResult instances executed by the framework.

Finally, make sure to update the app Xaml and change hellobotstrapper to simplebotstrapper. That's it. You can start and run MEF, and you can also handle some other key extension points of the bootstrapper.

Of course, you can use any IoC container you need as long as you provide the implementation of "GetInstance" and "GetAllInstances".

02 words for the wise

Although Caliburn Micro does provide ServiceLocator functionality through bootstrap overrides and IoC classes, but you should avoid using it directly in your application code. Service locator is considered an anti pattern by many people. Extracting from the container tends to obscure the intent of the dependent code and may make the test more complex.

In addition to what is shown above, there are other methods worth noting on the boot program. You can override OnStartup and OnExit to execute code when the application starts or closes, or you can override OnUnandledException to clear after any exceptions that the application code does not specifically handle.

03v4.0 changes

In 4.0, the bootstrapper saw some changes, that is, the DisplayRootViewFor method returned tasks and could wait for them.

protected Task DisplayRootViewFor<TViewModel>(IDictionary<string, object> settings = null)
{
    return DisplayRootViewForAsync(typeof(TViewModel), settings);
}

Use caliburn.com in Office and WinForms applications Micro

Caliburn.com can be used from non XAML hosts Micro. To achieve this, you must follow a slightly different process because your application is not through app Xaml started. Instead, a custom bootstrapper is created by inheriting bootstrapperbase (a non generic version). When inheriting, 'false' should be passed to the 'useApplication' parameter of the base constructor. This allows the bootstrapper to properly configure Caliburn. Exe without the XAML application instance Micro. All you need to do to start the framework is create an instance of the bootstrap and call the Initialize () method. Once the class is instantiated, you can use Caliburn as usual Micro, the new UI may be displayed by calling IWindowManager.

04 last

Original title: Caliburn Micro Xaml made easy

Original link: https://caliburnmicro.com/documentation/bootstrapper

dotnet programming Encyclopedia