How to have 2 commands to the same toolbar item?

EBatikEBatik NLMember ✭✭✭

Hi ppl! ToolbarItems.Add(new ToolbarItem("Save", null, () => (BindingContext as AddPersonViewModel).SaveCommand.Execute(null))); so that my toolbar item code. each time i press the save btn then savecommand.execute! i want to add one more command to this button. i would like its time i click the button 1st to save the data and then to navigate to anothe page ! How can i do that? Thanks

Best Answers

Answers

  • NMackayNMackay GBInsider, University mod
    edited September 2016

    You can however execute a bound command and raise a clicked event and both will fire but it seems wrong, your executing code in your view model and code behind with no way of controlling in what order the code will execute.

     <custom:CustomTabbedPage.ToolbarItems>
            <ToolbarItem Command="{Binding SendPOCommand}" Order="Primary" Priority="0" Clicked="MenuItem_OnClicked">
              <ToolbarItem.Icon>
                <OnPlatform x:TypeArguments="FileImageSource" iOS="IconSendPO" Android="IconSendPO" WinPhone="Images/IconSendPO.png" />
              </ToolbarItem.Icon>
            </ToolbarItem>
        </custom:CustomTabbedPage.ToolbarItems>
    
  • EBatikEBatik NLMember ✭✭✭

    @NMackay
    thank you for your reply! I ill try the way you discribed in the first comment and i ll let you know.

  • EBatikEBatik NLMember ✭✭✭

    @NMackay my code fails ofc! :neutral: how can i create a command inside a command?Could you help me with that!?

  • NMackayNMackay GBInsider, University mod
    edited September 2016

    @EBatik

    You could just create a second command in your viewmodel and execute it from the command bound to your control if you want to go down that path.

    Here's an example but I don't see why you'd want to do that.

    // This command is bound to the control and gets executed when the toolbar item is clicked.
    public RelayCommand<AppFavourite> SelectFavCommand
            {
                get
                {
                    return _selectFavCommand
                           ?? (_selectFavCommand = new RelayCommand<AppFavourite>(
                               async fav =>
                               {
                                   try
                                   {
                                       // This executes another command 1st
                                       DeleteFavCommand.Execute(fav);
    
                                         // Do logic in the 2nd command
    
  • EBatikEBatik NLMember ✭✭✭

    @NMackay are you using xlabs?

  • NMackayNMackay GBInsider, University mod

    No, I don't use XLabs.

  • WinterCloudWinterCloud GBMember ✭✭✭

    @NMackay said:
    @EBatik

    You could just create a second command in your viewmodel and execute it from the command bound to your control if you want to go down that path.

    Here's an example but I don't see why you'd want to do that.

    // This command is bound to the control and gets executed when the toolbar item is clicked.
    public RelayCommand<AppFavourite> SelectFavCommand
            {
                get
                {
                    return _selectFavCommand
                           ?? (_selectFavCommand = new RelayCommand<AppFavourite>(
                               async fav =>
                               {
                                   try
                                   {
                                     // This executes another command 1st
                                       DeleteFavCommand.Execute(fav);
    
                                       // Do logic in the 2nd command
    

    Why two commands? Any benefits of doing this?

    Why not just one command pointing to Method1, and Method1 calls Method2,3,4....?

  • NMackayNMackay GBInsider, University mod

    @WinterCloud

    Exactly, there is no benefit. That was my 1st comment.

  • WinterCloudWinterCloud GBMember ✭✭✭

    @NMackay said:
    @WinterCloud

    Exactly, there is no benefit. That was my 1st comment.

    Oh, sorry I went straight to code.

Sign In or Register to comment.