Toolbar Items break when using a ControlTemplate?

AndyHAndyH USMember ✭✭
edited April 2018 in Xamarin.Forms

I've been stuck for hours now so any handy hints would be very welcome!

I am using mvvm with a controltemplate and the page works fine with all commands.

I have attempted to add a settings icon to the navigation bar. It appears correctly and pressing the icon visibly changes, but it fails to trigger the command. If I remove the control template, the command works fine.

I've minimised my template down to just a stack layout:





Here is the toolbaritem that is rendered fine
<ContentPage.ToolbarItems>

</ContentPage.ToolbarItems>

My problem looks exactly the same as the comment in here by @BenoitRocco:
https://forums.xamarin.com/discussion/17880/toolbaritem-command-binding-doesnt-work

So ive tried adding a source but no joy:
Command="{Binding BindingContext.SettingsCommand, Source={x:Reference MainMenuView}}" />

I must be missing something obvious?
Can i definitely use a template on the whole page with toolbar items?

Thanks,
Andy

Best Answer

  • AndyHAndyH US ✭✭
    Accepted Answer

    In case anybody else gets the issue the problem is the timing of the command setup.
    It must be done in the ViewModel constructor and I was doing it immediately after the constructor.

    I also needed the source to be set in the binding (where MainMenuView is the x:Name of the view):
    Command="{Binding BindingContext.SettingsCommand, Source={x:Reference MainMenuView}}"

Answers

  • AndyHAndyH USMember ✭✭
    Accepted Answer

    In case anybody else gets the issue the problem is the timing of the command setup.
    It must be done in the ViewModel constructor and I was doing it immediately after the constructor.

    I also needed the source to be set in the binding (where MainMenuView is the x:Name of the view):
    Command="{Binding BindingContext.SettingsCommand, Source={x:Reference MainMenuView}}"

Sign In or Register to comment.