Page 1 of 1

[C++] Using commands

Posted: 25 Jan 2017, 08:19
by realesmedia
Command function doesn't call. Can't find solution.

XAML:
<Button x:Name="button1" Content="open project" Command="{Binding ButtonClick}"/> 
Source code:
class DelegateCommand : public BaseCommand {
public:
    typedef Delegate<void(BaseComponent*)> Action;
    DelegateCommand(const Action& action) : BaseCommand(NsSymbol::Null(), 0), _action(action) {}
    NsBool CanExecute(BaseComponent* param) const {
        return true;
    }
    void Execute(BaseComponent* param) const {
        _action(param);
    }
private:
    Action _action;

    NS_IMPLEMENT_INLINE_REFLECTION(DelegateCommand, BaseCommand) {
        NsMeta<TypeId>("DelegateCommand");
    }
}; 
class ProjectSelect : public BaseComponent {
public:
    ProjectSelect() {
        mCommand = *new DelegateCommand(MakeDelegate(this, &ProjectSelect::ButtonClick));
    }
    void ButtonClick(BaseComponent* sender) {
        // <<== Never access this point !!! =======
    }
    DelegateCommand* GetButtonClickCommand() const {
        return mCommand.GetPtr();
    }
private:
    Ptr<DelegateCommand> mCommand;

    NS_IMPLEMENT_INLINE_REFLECTION(ProjectSelect, BaseComponent) {
        NsMeta<TypeId>("ProjectSelect");
        NsProp("ButtonClick", &ProjectSelect::GetButtonClickCommand);
    }
}; 

Re: [C++] Using commands

Posted: 26 Jan 2017, 17:05
by sfernandez
Hi,

I've tested these classes and when a ProjectSelect instance is set as DataContext of the Button or one of its ancestors, then it works as expected: ButtonClick method is called when clicking on the Button.

You should check that your Button has the correct DataContext (the one who exposes the ButtonClick property).

Could you please verify that?

Re: [C++] Using commands

Posted: 27 Jan 2017, 12:48
by realesmedia
You should check that your Button has the correct DataContext (the one who exposes the ButtonClick property).
Could you please verify that?
Thanks. I didn't specify DataContext property in XAML. Problem is solved.

Re: [C++] Using commands

Posted: 28 Jan 2017, 00:26
by jsantos
Thanks for the feedback!