-3
\$\begingroup\$
    private readonly ICommand onSelectIssueCommand = null;

    public ICommand OnSelectedIssueCommand
    {
        get { return onSelectIssueCommand ?? new Command<object>(OnEventSelected); }
    }

I'm trying to find out the best way to initialize commands, I really like this way but someone recently told me this was wrong. I haven't had any problems with it whatsoever but I would like to learn the best way to do it. Mainly use commands in xamarin-mobile development context

\$\endgroup\$
  • \$\begingroup\$ You need to provide more context. \$\endgroup\$ – t3chb0t Mar 16 '18 at 15:17
  • \$\begingroup\$ What more would you like to know? I'm just wondering if this is an acceptable way to initalize my commands? \$\endgroup\$ – JsonDork Mar 16 '18 at 15:19
  • \$\begingroup\$ But this isn't initializing anything. onSelectIssueCommand always remains null. \$\endgroup\$ – t3chb0t Mar 16 '18 at 15:21
  • \$\begingroup\$ Really? But then it shouldn't be working at all. Maybe it gets initalized during the xamarin initalizecomponent stage? Seems like that always enters it. \$\endgroup\$ – JsonDork Mar 16 '18 at 15:25
0
\$\begingroup\$

Isn't this way always creating a new Command when the getter is called ?

I would have done something like this :

private ICommand onSelectIssueCommand = null;

public ICommand OnSelectedIssueCommand
{
    get 
    { 
        if (onSelectIssueCommand == null)
            onSelectIssueCommand = new Command<object>(OnEventSelected);
        return onSelectIssueCommand;  
    }
}
\$\endgroup\$
  • \$\begingroup\$ Technically it is called every time the getter is called, but shoudln't the getter only be called once during initalization of pages? \$\endgroup\$ – JsonDork Mar 16 '18 at 15:21
  • \$\begingroup\$ You may need to call it again if you need to raise the CanExecuteChanged event. \$\endgroup\$ – Mohammed Larabi Mar 16 '18 at 15:23
  • \$\begingroup\$ Good Point! Didn't think about that \$\endgroup\$ – JsonDork Mar 16 '18 at 15:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.