• Setting Window Size With Caliburn Micro

    This is something that has actually bugged me for a while. Once I figured it out, it annoyed me that I didn’t figure it out sooner.

    When displaying a window in Caliburn.Micro, you can set attributes about the Window object when calling it.

    So, let’s say you want to set the height and width on the window to 600 x 300:

    First, you would start with something like this:

    public class ShellViewModel : PropertyChangedBase, IShell
    {
        private readonly IWindowManager windowManager;
    
        public ShellViewModel()
        {
            this.windowManager = new WindowManager();
            this.windowManager.ShowWindow(new LameViewModel());
        }
    }

    There are two other fields on the ShowWindow method. The third parameter lets you dynamically set the attributes on the Window object.

    public class ShellViewModel : PropertyChangedBase, IShell
    {
        private readonly IWindowManager windowManager;
    
        public ShellViewModel()
        {
            this.windowManager = new WindowManager();
    
            dynamic settings = new ExpandoObject();
            settings.Height = 600;
            settings.Width = 300;
    
            this.windowManager.ShowWindow(new LameViewModel(), null, settings);
        }
    }

    I wish there was more information about working with this on the documentation, but there you have it.

  • Setting up Autofac with Caliburn Micro v3.2.0 and Autofac v4.8.1

    I find myself frequently setting up new projects with Caliburn Micro; however, It isn’t always easy to remember the code to integrate Autofac with the bootstrapper. So, here is the template that I use when creating a new application.

    public class ClientBootstrapper : BootstrapperBase
    {
        private static IContainer Container;
    
        public ClientBootstrapper()
        {
            this.Initialize();
        }
    
        protected override void Configure()
        {
            var builder = new ContainerBuilder();
    
            builder.RegisterType<WindowManager>()
                .AsImplementedInterfaces()
                .SingleInstance();
    
            builder.RegisterType<EventAggregator>()
                .AsImplementedInterfaces()
                .SingleInstance();
                
            Container = builder.Build();
        }
    
        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            var type = typeof(IEnumerable<>).MakeGenericType(service);
            return Container.Resolve(type) as IEnumerable<object>;
        }
    
        protected override object GetInstance(Type service, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                if (Container.IsRegistered(service))
                    return Container.Resolve(service);
            }
            else
            {
                if(Container.IsRegisteredWithKey(key, service))
                    return Container.ResolveKeyed(key, service);
            }
    
            var msgFormat = "Could not locate any instances of contract {0}.";
            var msg = string.Format(msgFormat, key ?? service.Name);
            throw new Exception(msg);
        }
    
        protected override void BuildUp(object instance)
        {
            Container.InjectProperties(instance);
        }
    }

    Hope this helps you out! Good luck with your WPF application.