ひとつひとつ解説すると長くなるので、使い慣れた構成で淡々とプロジェクトを開始できるようにというメモにする(更新するかも)

概要

以下の構成でプロジェクトを開始する。

  • Caliburn.Micro (MVVMライブラリ) → ガイド
  • Autofac (DIコンテナ) → ガイド
  • Autofac.AttributedComponent (Autofacへのコンポーネント収集)
  • ReactiveProperty + Rx(Model/ViewModel間の連携)

プロジェクトの作成とパッケージインストール

  • WPFプロジェクトを作る

  • 以下をInstall-Package

    • Caliburn.Micro
    • Autofac.AttributedComponent
    • ReactiveProperty

初期View / ViewModelの作成

ViewModels/ShellViewModel.csにViewModelを作成、Views/ShellView.xamlにWindowを作成。
(フォルダは切らなくてもいい: View / View Model Naming Conventionsを参照)

ShellViewModelはCaliburn.Micro.PropertyChangedBaseを継承し、Component attributeを付与する(Scope=ComponentScope.SingleInstanceにするかは好みで)

ShellViewModel.cs
    [Component]
    public class ShellViewModel : Caliburn.Micro.PropertyChangedBase
    {
    }

ShellView.csのクラスにも[Component]をつける。
(つけなくてもいいが、[Resource]によるフィールド・インジェクションが可能になるので、これによってビュー向けのシングルトンインスタンスを受け取ると便利。)

AppBootstrapperの作成

Caliburn.Micro初期化用クラスをBootstrapperBaseを継承して作る。

Customizing The Bootstrapper を参照。Caliburn.Micro.AutofacBootstrap がそこそこやってくれるのだけど、ちょっとばかり微妙なので、Autofacへのつなぎは以下で済ませる。

AppBootstrapper.cs
    public class AppBootstrapper : BootstrapperBase
    {
        public AppBootstrapper()
        {
            Initialize();
        }

        private IContainer container;

        protected override void Configure()
        {
            var builder = new Autofac.AttributedComponent.AttributedContainerBuilder();
            builder.RegisterAssemblyTypesWithAttribute(typeof(AppBootstrapper).Assembly);

            // TODO: 必要に応じて取り替える
            builder.ContainerBuilder.Register(c => new WindowManager()).As<IWindowManager>();
            builder.ContainerBuilder.Register(c => new EventAggregator()).As<IEventAggregator>();

            // TODO: 必要に応じてコンポーネントを追加する
            container = builder.Build();
        }

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            // TODO: 初期化コードを追記
            DisplayRootViewFor<ShellViewModel>();
        }

        protected override void OnExit(object sender, EventArgs e)
        {
            container.Dispose();
        }


        protected override object GetInstance(Type service, string key)
        {
            return string.IsNullOrWhiteSpace(key) ?
                container.Resolve(service) :
                container.ResolveNamed(key, service);
        }

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

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

App.xamlの設定

以下のようにする。StartupUriは削除する。

App.xaml
<Application x:Class="(名前空間).App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:(名前空間)">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:AppBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml(.cs)の削除

いらないので削除。

おまけ: お試し

試したかったら、まぁこんな感じで(Basic Configuration, Actions and Conventionsより)

ShellView.xaml追記

Shell.xaml
        <StackPanel>
            <TextBox x:Name="Name" />
            <Button x:Name="SayHello"
                Content="Click Me" />
        </StackPanel>

ShellViewModel.cs

ShellViewModel.cs
        string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyOfPropertyChange(() => Name);
                NotifyOfPropertyChange(() => CanSayHello);
            }
        }

        public bool CanSayHello
        {
            get { return !string.IsNullOrWhiteSpace(Name); }
        }

        public void SayHello()
        {
            System.Windows.MessageBox.Show(string.Format("Hello {0}!", Name)); //Don't do this in real life :)
        }