Are WPF routing commands removed or degrading?

From what I understand, the purpose of the Command pattern is to help separate the interaction of the user interface with the application logic. If the commands are executed correctly, clicking on the "Print" menu item can lead to an interaction chain as follows:

(button) ---click executes command----> (command) ---calls Print() in app logic ---> (logic)

This encourages you to separate the user interface from the application logic.

I watched WPF commands, and for the most part I can see how they implemented this template. However, I feel that to some extent they complicated the Command pattern and were able to implement it in such a way that you are not recommended to separate the interface from the application logic.

For example, consider this simple WPF window, which has a button for inserting text into a text field:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Paste"
                        Executed="CommandBinding_Executed"/>
    </Window.CommandBindings>
    <StackPanel>
        <TextBox x:Name="txtData" />
        <Button Command="Paste" Content="Paste" />
    </StackPanel>
</Window>

Here is the code:

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ApplicationCommands.Paste.Execute(null, txtData);
        }
    }
} 

? , Click. , Paste, , , ? . , , :

(button) ---executes Routed Command---> (Window) ---executes command binding----(command binding)
(logic) <---calls application logic--- (event handler) <-----raises event --------------|

? .

+5
4

, Paste, CommandTarget CommandParameter. TextBox, CommandTarget.

, RoutedCommand . Executed , .

+2

.

ICommand . .

ICommand, . , , , . , /. , , .

+3

RoutedCommands RoutedUICommands . , TextBox UndoCommand , guseture Ctrl + Z. , ICommand Execute CanExecute. Command . view/xaml , Execute/CanExecute. .

. () InputBindings. - Microsoft , !

+2

They may be excessive for some things, but you get some useful advantages, such as CanExecute, which can automatically enable / disable buttons / menu items when a command is unavailable (for example, text is not selected, etc.). You can also execute commands in Blend without using any code, which is great for designers.

0
source

All Articles