私は、コンストラクタ内CompositeCommandをインスタンス化し、次のViewModelを持っている:CompositeCommandの制御を反転するには?
public class ViewImportPreviewViewModel:BindableBase
{
private IEventAggregator eventAggregator; //event aggregator to enable Studio button in different ViewModel
private readonly IRegionManager regionManager; //region manager for navigation to the main menu
public CompositeCommand FinalizeImportClick{get;set;}//composite command to register multiple command for finalize import button click
public ViewImportPreviewViewModel(IRegionManager regionManager, IEventAggregator eventAggregator) //constructor
{
this.eventAggregator = eventAggregator;
this.regionManager = regionManager;
FinalizeImportClick = new CompositeCommand();
FinalizeImportClick.RegisterCommand(new DelegateCommand<string>(NavigateToMain)); //register a delegate command for finalize import button click
}
//subscriber method to the firs delegate command registered with finalize button click
private void NavigateToMain(string argument)
{
//it enables the studio button after import and sends it to the main menu view XAML
eventAggregator.GetEvent<ButtonEnableEvent>().Publish("ImportDone");
//it navigates to the main menu after import
regionManager.RequestNavigate("ScreenNavigationRegion", argument);
//publish an event for the main buttons viewmodel and then over there try to fade in main buttons
eventAggregator.GetEvent<FadeinButtonsEvent>().Publish("ImportDone");
}
}
は今、私は制御の反転を行うには、クラスの外CompositeCommandをインスタンス化し、このようにユニティコンテナを通してそれを注入します:
public class ViewImportPreviewViewModel:BindableBase
{
private IEventAggregator eventAggregator; //event aggregator to enable Studio button in different ViewModel
private readonly IRegionManager regionManager; //region manager for navigation to the main menu
public CompositeCommand finalizeImportClick;//composite command to register multiple command for finalize import button click
public ViewImportPreviewViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, CompositeCommand finalizeImportClick) //constructor
{
this.eventAggregator = eventAggregator;
this.regionManager = regionManager;
this.finalizeImportClick = finalizeImportClick;
finalizeImportClick.RegisterCommand(new DelegateCommand<string>(NavigateToMain)); //register a delegate command for finalize import button click
}
//subscriber method to the firs delegate command registered with finalize button click
private void NavigateToMain(string argument)
{
//it enables the studio button after import and sends it to the main menu view XAML
eventAggregator.GetEvent<ButtonEnableEvent>().Publish("ImportDone");
//it navigates to the main menu after import
regionManager.RequestNavigate("ScreenNavigationRegion", argument);
//publish an event for the main buttons viewmodel and then over there try to fade in main buttons
eventAggregator.GetEvent<FadeinButtonsEvent>().Publish("ImportDone");
}
}
、その後、モジュールの初期化に、私はこれを実行します。
CompositeCommand myCommand = new CompositeCommand();
container.RegisterInstance<CompositeCommand>(myCommand);
をIエラーは発生しませんが、コマンドが登録されても、NavigateToMain(string argument)
は呼び出されません。
代わりにタイプを登録しようとしましたが、CompositeCommandはRegisterCommand
メソッドの定義を含まないICommandインターフェイスを継承しています。
は私の最初の質問は、あなたが最初の場所でこれを実行したいと思う理由は、ありますか?ユニットテストですか?ビューモデルの外で初期化したい理由が他にはありません。また、 'CompositeCommand'はシングルトンであり、作成されたすべてのビューで渡され、登録され、これによってコンポジットコマンドはあなたのviewmodelへの参照を保持するので、あなたの試みはかなり危険です。 'CompositeCommand'のライフタイムがアプリケーションのものと等しいので、ビューモデルはいつもガベージコレクションされません – Tseng
正直言って、私は何か特別な理由はありませんこの。私はちょうど固体の原則に従おうとしていました。 – Ivan