1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Prism コードサンプル学習:12-UsingCompositeCommands

Posted at

Prism コードサンプル学習:12-UsingCompositeCommands

はじめに

以下の記事の続きです。
https://qiita.com/mngreen/items/95dd62ee8086887f5d0b

12-UsingCompositeCommands

本サンプルでは、CompositeCommandsの実装方法を紹介しています。
Compositeという名前の通り、CompositeCommandが実行されるとそのコマンドに登録されている複数のDelegateCommandが実行される仕組みになっています。

  • エントリーポイントでは、ApplicationCommandsをシングルトンで登録しています。

      namespace UsingCompositeCommands
      {
          public partial class App : PrismApplication
          {
              ...
    
              protected override void RegisterTypes(IContainerRegistry containerRegistry)
              {
                  containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
              }
          }
      }
    
  • CompositeCommandsでは複数モジュールから利用されること意識してか、別パッケージ管理となっていました。以下が実際の定義です。

    namespace UsingCompositeCommands.Core
    {
        public interface IApplicationCommands
        {
            CompositeCommand SaveCommand { get; }
        }
    
        public class ApplicationCommands : IApplicationCommands
        {
            private CompositeCommand _saveCommand = new CompositeCommand();
            public CompositeCommand SaveCommand
            {
                get { return _saveCommand; }
            }
        }
    }
    
  • 実際に、xamlにバインディングするときには通常のコマンドと同様にバインディングするだけでよいように作られています。

    <Window x:Class="UsingCompositeCommands.Views.MainWindow" ...>
    
      ...
      
      <Grid>
          <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="*" />
          </Grid.RowDefinitions>
    
          <Button Content="Save" Margin="10" Command="{Binding ApplicationCommands.SaveCommand}"/>
    
          <TabControl Grid.Row="1" Margin="10" prism:RegionManager.RegionName="ContentRegion" />
      </Grid>
    </Window>
    
  • CompositeCommands実行時に実行してほしいコマンドは、以下のように登録しておく必要があります。

    
    namespace ModuleA.ViewModels
    {
        public class TabViewModel : BindableBase
        {
            ...
    
            public TabViewModel(IApplicationCommands applicationCommands)
            {
                _applicationCommands = applicationCommands;
    
                UpdateCommand = new DelegateCommand(Update).ObservesCanExecute(() => CanUpdate);
    
                _applicationCommands.SaveCommand.RegisterCommand(UpdateCommand);
            }
    
            private void Update()
            {
                UpdateText = $"Updated: {DateTime.Now}";
            }       
        }
    }
    
  • CompositeCommandsの定義はここにあります。

    • CompositeCommands.RegisterCommandはここにあります。ICommand型のリストにコマンドを詰め込んでいきます。
    • CompositeCommands.Executeでは、いったんキューに詰めなおし、そのキューが空になるまでループさせています。
      • 想像でしかありませんが、キューに詰めなおすタイミングでlockしていることから、非同期でも操作されても困らないようにしているのだと思われます。

おわりに

今回はCompositeCommandsの利用方法を学びました。
このコマンドを利用すれば、閉じたりした場合に、一括で初期化できたりしそうですね。
次回、13-IActiveAwareCommandsについて見ていこうと思います。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?