2017-02-20 7 views
0

たとえば、デフォルトwpfクラスのRoutedCommand、CommandBinding、ICommandインターフェイスを使用することはできません。代わりに私自身の実装を提供する必要があります。 だから私はシンプルなのICommandインターフェイスを指定します。WPFでのコマンドパターンの実装

public interface ICommand 
{ 
    void Execute(); 
} 

特定のコマンドのためのクラスを作成します。

public class NewCommand : ICommand 
{ 
    private readonly IReceiver _receiver; 
    // receiver is any class that provides implementation of a New() method 

    public NewCommand(IReceiver receiver) 
    { 
     _receiver = receiver; 
    } 
    public void Execute() 
    { 
     _receiver.New(); 
    } 
} 

同様の発動が存在する必要があります:

public class Invoker 
{ 
    private ICommand _command; 
    public void SetCommand(ICommand c) 
    { 
     _command = c; 
    } 
    public void Run() 
    { 
     _command.Execute(); 
    } 
} 

どのように私はこのすべてを配線しませんデフォルトの実装では私のコードは次のようになります:

新しいコマンドを呼び出すボタンは次のようにバインドされます。コマンド属性はSystem.Windows.Input.ICommandを実装するクラスを想定した場合

<Button Margin="3" Width="55" Style="{StaticResource ToolBarButtonBaseStyle}" 
        Command="{x:Static ApplicationCommands.New}" 
        CommandTarget="{Binding ElementName=MyDesigner}"> 
       <Button.Content> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="4*"/> 
          <RowDefinition Height="1*"/> 
         </Grid.RowDefinitions> 
         <Image Source="Images/GenericDocument.png" Width="45"/> 
         <TextBlock Grid.Row="1" Text="New" VerticalAlignment="Bottom" HorizontalAlignment="Center"/> 
        </Grid> 
       </Button.Content> 
      </Button> 

どのように私は私のNewCommandクラスをバインドするのですか? Invokerのインスタンスを作成するには、どこにNewCommandを設定して実行しますか?一般に、パターンのデフォルトの実装を自分のものに置き換える方法について少し混乱しています。アドバイスは大歓迎です。

+2

これは意味をなさないと思われます。ボタンの 'Command'プロパティに割り当てられたオブジェクトは' System.Windows.Input.ICommand'を実装しなければなりません。 – Clemens

+0

私のカスタムICommandインターフェイスは、System.Windows.Input.ICommandから継承する必要がありますか?または、Click属性を使用して回避策がありますか? – eXPerience

+0

"Command属性でSystem.Windows.Input.ICommandを実装するクラスが必要な場合、どのようにnewCommandクラスをバインドするのですか?"できません。 ButtonのCommandプロパティを、組み込みのICommandインターフェイスを実装するオブジェクトに設定する必要があります。 – mm8

答えて

1

カスタムコマンドインターフェイスと実装を実際に持っているのは間違いですが、おそらくClickハンドラをPropertyChangedCallbackにアタッチするカスタムアタッチCommandプロパティを作成できます。 Clickハンドラはカスタムコマンドを実行します。

次のコードサンプルでは、​​カスタムICustomCommandインターフェイスを宣言し、という静的クラスに接続されたCommandプロパティを宣言しています。

public interface ICustomCommand 
{ 
    void Execute(); 
} 

public static class CustomCommandEx 
{ 
    public static readonly DependencyProperty CommandProperty = 
     DependencyProperty.RegisterAttached(
      "Command", 
      typeof(ICustomCommand), 
      typeof(CustomCommandEx), 
      new PropertyMetadata(CommandPropertyChanged)); 

    public static ICustomCommand GetCommand(DependencyObject obj) 
    { 
     return (ICustomCommand)obj.GetValue(CommandProperty); 
    } 

    public static void SetCommand(DependencyObject obj, ICustomCommand value) 
    { 
     obj.SetValue(CommandProperty, value); 
    } 

    private static void CommandPropertyChanged(
     DependencyObject obj, DependencyPropertyChangedEventArgs eventArgs) 
    { 
     var button = obj as ButtonBase; 
     var command = eventArgs.NewValue as ICustomCommand; 

     if (button != null) 
     { 
      button.Click += (s, e) => command.Execute(); 
     } 
    } 
} 

あなたはこのように添付プロパティを割り当てます:SomeCommandICustomCommandを実装し、ビューモデルのプロパティです

<Button local:CustomCommandEx.Command="{Binding SomeCommand}"/> 

関連する問題