0

I have a delete object command working in my ViewModel. The command is bound to the button. So far, so good. When I click the button the object is deleted from the db. Now, obviously, this needs to be validated, so I am using DisplayAlert to show a "Are you sure you want to delete?" to the user. This displays, but the item deletes anyway on the button click which makes sense. I am struggling to understand how to only run my command in the ViewModel if the user chooses "Yes".

XAML Button:

 <Button Text="Delete player" Command="{Binding DeleteCommand}" Clicked="DeletePlayerEvent"></Button>

Clicked Event:

 private async void DeletePlayerEvent(object sender, EventArgs e)
    {
        bool answer = await DisplayAlert("Notification", "Are you sure you want to delete this player?", "Yes", "No");
        //if (answer == true)??????
       // {
            //Can I run the command from here?
        //}
    }

ViewModel Command

 public Command DeleteCommand
    {
        get
        {
            return new Command(async () =>
            {
                var playerServices = new PlayerServices();
                await playerServices.DeletePlayerAsync(_Player.Id);
            });
        }
    }

1 Answer 1

2

The problem here is that you are doing Command binding and Event subscribing together.

These both are getting fired individually and not dependent to each other.

One way you can wire them up together is call the ViewModel's DeleteCommand from your button click event when the user selected "Yes"

To get hold of the DeleteCommand property in your click event (assuming that is written in your codebehind file):

var viewModel = this.BindingContext as YouViewModel
viewModel.DeleteCommand.Execute();

Hope that helps!

Sign up to request clarification or add additional context in comments.

5 Comments

This is a quick fix but I need to call out that it's not benefiting from proper MVVM - the view model is supposed to hold the logic of the view and the view should be presentation only. Imagine if the dialog needs to show based on multiple view model properties or after execution of the command failed (eg try again). Half of your view logic will live in the view and can't be unit tested
@bryanbcook - Please don't stress on the MVVM much. In Xamarin you should do some stuffs in view's code behind too. That is not crime. This is a debatable topic and I would step back to avoid it. I am not taking stand on it. Even the best known Xamarin developers (James) do it. You can look for the same in github.com/xamarinhq/app-evolve showcased in Xamarin Evolve 2016. If you think that is wrong, I would highly insist you to open a bug on that repo and send me a link to chime in. I would be glad to talk in detail.
And @bryanbcook - do you have a suggestion / alternate suggestion to make that would work better than a "quick" fix?
I"m also not saying it's wrong; I just don't want beginners to assume this is the right approach. I've worked on teams that have put abstractions around message dialogs (similar to every MVVM framework on the planet); For Xamarin i recently wrote a behavior that shows an ActionSheet. It's always possible, it just requires a bit more thinking if unit testability is your main objective of MVVM.
In programming there is no 1 approach to do something. So right or wrong would be misleading. You do what you got to do. I have worked on teams who did follow MVVM religiously and others who pick the stick. Try to concentrate on the question asked where the point was to tie Command and EventHandler together and nothing to do with big picture of MVVM framework, abstractions, unit testability. Concerns can be expressed by jotting down an answer explaining things. I would encourage to end up our small discussion here to keep it clean, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.