Unity を WPF MVVM アプリケーションに実装しようとしていますが、全体像がわかりません。
現時点では、次のようなブートストラップを作成しました。
public class MainBootstrapper : Bootstrapper<MainViewModel>
{
private UnityContainer container;
protected override void Configure()
{
container = new UnityContainer();
container.RegisterType<IServiceLocator, UnityServiceLocator>(new ContainerControlledLifetimeManager());
container.RegisterType<IWindowManager, WindowManager>(new ContainerControlledLifetimeManager());
container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());
}
protected override object GetInstance(Type service, string key)
{
if (service != null)
{
return container.Resolve(service);
}
if (!string.IsNullOrWhiteSpace(key))
{
return container.Resolve(Type.GetType(key));
}
return null;
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.ResolveAll(service);
}
protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
}
これをどのように使用するのが最善の方法ですか?このコードは現在動作します:
public class MainViewModel : PropertyChangedBase
{
public MainViewModel()
{ }
[Dependency]
public Sub1ViewModel Sub1VM { get; set; }
[Dependency]
public Sub2ViewModel Sub2VM { get; set; }
}
MainView には次のものがあります。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl Grid.Row="0" Name="Sub1VM" />
<ContentControl Grid.Row="1" Name="Sub2VM" />
</Grid>
まず第一に、私が共有したコードは、Unity + Caliburn を使用する正しい方法ですか?
ここで、私の Sub1VM がモデル 'M1' を使用するとしますが、Sub2VM は同じモデルを使用して情報を表示する必要がありますが、モデル M1 の別のインスタンスを作成する必要はありません。(シングルトン)
これは今どのように機能しますか?表示 各ビューモデル コンストラクターで IServiceLocator を使用しますか? 誰かがそれを説明するためにコードサンプルを共有できますか?