私の連絡先情報
郵便メール:
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
複合コマンドは、登録されている複数のコマンドをトリガーし、一度に複数のコマンドを実行できます。すべて保存するなどのビジネス要件を実現できます。
1. ICompositeCommands インターフェイスと CompositeCommands 実装クラスを作成します。
Prism で提供される CompositeCommand オブジェクトを ICompositeCommands インターフェイスと ApplicationCommands 実装クラスにラップします。
public interface IApplicationCommands
{
CompositeCommand SaveCommand { get; }
}
public class ApplicationCommands : IApplicationCommands
{
private CompositeCommand _saveCommand = new CompositeCommand();
public CompositeCommand SaveCommand
{
get { return _saveCommand; }
}
}
2. 複合コマンドをIOCに登録する
App.xaml バックグラウンド コードで IOC 登録を実行する
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
}
3. ViewModel で複合コマンドを定義する
ViewModel で複合コマンドを定義し、そのコマンドを View レイヤーにバインドします。
ビューモデル:
private IApplicationCommands _applicationCommands;
public IApplicationCommands ApplicationCommands
{
get { return _applicationCommands; }
set { SetProperty(ref _applicationCommands, value); }
}
public MainWindowViewModel(IApplicationCommands applicationCommands)
{
ApplicationCommands = applicationCommands;
}
ビュー:
<Button Content="Save" Margin="10" Command="{Binding ApplicationCommands.SaveCommand}"/>
4. 複合コマンドを他の複数の場所のコマンドに登録します。
public TabViewModel(IApplicationCommands applicationCommands)
{
_applicationCommands = applicationCommands;
UpdateCommand = new DelegateCommand(Update).ObservesCanExecute(() => CanUpdate);
_applicationCommands.SaveCommand.RegisterCommand(UpdateCommand);
}
private void Update()
{
UpdateText = $"Updated: {DateTime.Now}";
}
このようにして、複合コマンド ApplicationCommands.SaveCommand がトリガーされると、登録されているすべてのコマンドがトリガーされます。