假設我不允許使用默認的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();
}
}
到XAML,考慮到默認實現,我的代碼如下所示:
public class DesignerCanvas : Canvas
{
private readonly IDesignerCommandsReceiver _receiver;
public DesignerCanvas()
{
_receiver = new DesignerCommandsReceiver(this);
CommandBindings.Add(new CommandBinding(ApplicationCommands.New, New_Executed));
}
private void New_Executed(object sender, ExecutedRoutedEventArgs e)
{
_receiver.New();
}
}
和調用新的命令按鈕被綁定,如:
<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類,如果命令屬性,預計實現System.Windows.Input.ICommand類?我在哪裏創建Invoker的實例,設置NewCommand並運行它?一般來說,我對如何用自己的模式替換模式的默認實現有點困惑。任何建議,歡迎。
這似乎沒有道理。分配給Button的'Command'屬性的對象必須實現'System.Windows.Input.ICommand'。 – Clemens
也許我的自定義ICommand接口應該從System.Windows.Input.ICommand繼承?或者有使用Click屬性的工作? – eXPerience
「如果Command屬性需要實現System.Windows.Input.ICommand的類,那麼如何綁定我的NewCommand類?」你不能。您必須將Button的Command屬性設置爲實現內置ICommand接口的對象。 – mm8