简体   繁体   中英

how does the command pattern works?

i am doing :

       <Button 
            Style="{StaticResource buttonstyle}"
            HorizontalAlignment="Left" Margin="12,21,10,0" 
            VerticalAlignment="Top" Height="78" Width="83" 
            BorderThickness="2" Content="add event"
            Command="{Binding NewEvent}" 
            CommandParameter="This is the report."
        >
        </Button>

where command is :

   public class StringDelegateCommand : ICommand 
{ 
    //methodes without return value
    Action<string> m_ExecuteTargets = delegate { };
    //methodes without parmtters inside
    Func<bool> m_CanExecuteTargets = delegate { return false; };
    //the value whom allows execution 
    bool m_Enabled = false;


    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        Delegate[] targets = m_CanExecuteTargets.GetInvocationList();
        foreach (Func<bool> target in targets)
        {
            m_Enabled = false;
            bool localEnable = target.Invoke();
            if (localEnable)
            {
                m_Enabled = true;
                break;
            }
        }
        return m_Enabled;
    }

    public event EventHandler CanExecuteChanged = delegate { };

    public void Execute(object parameter)
    {
        if (m_Enabled)
            m_ExecuteTargets(parameter != null ? parameter.ToString() : null);
    }

    #endregion

    public event Action<string> ExecuteTargets
    {
        add
        {
            m_ExecuteTargets += value;
        }
        remove
        {
            m_ExecuteTargets -= value;
        }
    }

    public event Func<bool> CanExecuteTargets
    {
        add
        {
            m_CanExecuteTargets += value;
            CanExecuteChanged(this, EventArgs.Empty);
        }

        remove
        {
            m_CanExecuteTargets -= value;
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }
}

and in my view model (which is binded in context so things do bind !!!):

 //called in ctor where newEvent is defined : StringDelegateCommand newEvent; 
     private void setNewEventCommand()
    {
        newEvent = new StringDelegateCommand();
        newEvent.CanExecuteTargets += isThereAnotherNewEvent;
        newEvent.ExecuteTargets += exacuteNewEvent;
        NewEvent = newEvent; 
    }

    void exacuteNewEvent(string message)
    {
        Window1 w = new Window1();
        w.ShowDialog();

    }

When I click on the button nothing happenes , what am I doing wrong ?can someone help me understand my error...

thank you ...

EDIT i should mention this is what the compliler writes :

System.Windows.Data Error: 40 : BindingExpression path error: 'NewEvent' property not found on 'object' ''viewModel' (HashCode=18612316)'. BindingExpression:Path=NewEvent; DataItem='viewModel' (HashCode=18612316); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')

First of all, check if your binding binds at all. You can see binding errors in the Output window of Visual Studio at runtime. Or you can make somewhere a breakpoint, and look in the debugger what is the value of the button's Command (you would need to give a name to the button, in order to easily access it in the debugger: <Button x:Name="TEST" will allow to access TEST as a variable in your Window/Control).

Second, if the binding is correct, you should set a breakpoint in CanExecute and Execute and see what exactly happens.

Hope that helps.

问题得到了解决:该物业应已公开,然后一切正常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM