0
votes

Hi i'm working on a pretty simple xamarin app using the latest version of prism and dryioc. I'm building the app after specifications from a client who really wants to use toolbar items.

However i have never used them together with mvvm patterns and i'm having trouble finding a elegant way to bind everything together without "hacks" and quick fixes.

I heard from somewhere that the ToolbarItems page property is bindable but i've tried multiple times without success.

Is there a way to bind ToolbarItems from a viewmodel? Thanks in advance.

1
Maybe you should read about what to ask here at SO. We don't suggest. We answer concerete questions - Eser

1 Answers

0
votes

I didn't use this "dryioc" before but it's easy to bind toolbar items.

first, add the XAML according to the type of page you are in:

 <ContentPage.ToolbarItems>
    <ToolbarItem Name="Cart" Command="{Binding GoToCartCommand}" Icon="Icons/cart.png">
    </ToolbarItem>
</ContentPage.ToolbarItems>

after that, in the view model, add the 2 commands as following:

Command _goToCartCommand;

    public Command GoToCartCommand
    {
        get { return _goToCartCommand; }
        protected set { _goToCartCommand = value; }
    }

Notice that we used the command with Big letter in XAML After that in the constructor of the view model initialize the command, notice that you add only the name of the function. (you can add parameter with simple modification but it's usually not needed):

_goToCartCommand = new Command(GoToCartClicked);

Finally, Add your function:

 async void GoToCartClicked()
    {
        await _pageDialogService.DisplayAlertAsync("Confirmation", "You are going to the cart page.", "OK");

    }