これは簡単なことですが...実際にはサンプルには含まれていません!
ViewModelLocatorsの「コンテナ」はMvxApplicationオブジェクトです。デフォルトでは、MvxDefaultViewModelLocator
という規約を使用して、文字列パラメータを持つ宣言されたコンストラクタを使用してViewModelインスタンスを構築しようとします。
例:
public class MyViewModelLocator : MvxViewModelLocator
{
public MyFirstViewModel CreateFirst()
{
return new MyFirstViewModel();
}
public MySecondViewModel CreateSecond(string aParameter)
{
var someLookup1 = ComplicatedStaticThing1.Lookup(aParameter);
var viewModel = new MySecondViewModel(someLookup1);
var someLookup2 = ComplicatedStaticThing2.Lookup(aParameter, someLookup1);
viewModel.DoSomething(someLookup2);
return viewModel;
}
private readonly MyThirdViewModel _third = new MyThirdViewModel();
public MyThirdViewModel Third
{
get
{
return _third;
}
}
}
あなたがあなた自身のViewModelロケータを使用したい場合は、最も簡単な方法は、MvxViewModelLocatorを継承し、公共のプロパティまたはあなたのViewModelのインスタンスを返すパブリックメソッドのいずれかを提供することは簡単です
これよりも下位に移動する場合は、代わりにIMvxViewModelLocatorを直接実装することもできます。
単純にアプリ内でそれをインスタンス化し、追加し、アプリケーションにViewModelLocatorを追加するには - 例えば:
public class App
: MvxApplication
, IMvxServiceProducer<IMvxStartNavigation>
{
public App()
{
this.RegisterServiceInstance<IMvxStartNavigation>(new StartApplicationObject());
base.AddLocator(new MyViewModelLocator());
// to disable the default ViewModelLocator, use:
// base.UseDefaultViewModelLocator = false;
}
}
注: - 離れて、設計時のデータ用から、私は今まれ非常にカスタムViewModelLocatorを実装する必要があることを知ってください。一般的に、私がやりたいことはViewModelの構築内で行うことができます。