realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

[C++] Using commands

25 Jan 2017, 08:19

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);
    }
}; 
Last edited by realesmedia on 27 Jan 2017, 13:12, edited 1 time in total.
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: [C++] Using commands

26 Jan 2017, 17:05

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?
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: [C++] Using commands

27 Jan 2017, 12:48

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.
 
User avatar
jsantos
Site Admin
Posts: 4124
Joined: 20 Jan 2012, 17:18
Contact:

Re: [C++] Using commands

28 Jan 2017, 00:26

Thanks for the feedback!

Who is online

Users browsing this forum: Google [Bot] and 0 guests