Kevin Flynn
Kevin Flynn

Reputation: 3

How to call Method from Xamarin XAML Code in Button (MVVM)

Aloha,

i want to call a method (written in Model) on Button Click but without Events. We're working with MVVM and i want to bind the method on the specific button. Already tested it on that way:

<Button Grid.Row="0" Grid.Column="3" Text="Next" Grid.RowSpan="3" BackgroundColor="Red" Command="EntryOkNext" />

It doesn't do anything. Something missing?

Upvotes: 0

Views: 1337

Answers (1)

haldo
haldo

Reputation: 16711

Assuming EntryOkNext is a command in the ViewModel, you just need to bind the command using the correct syntax: Command="{Binding EntryOkNext}".

<Button Grid.Row="0" 
        Grid.Column="3" 
        Text="Next" 
        Grid.RowSpan="3" 
        BackgroundColor="Red" 
        Command="{Binding EntryOkNext}" />

There are many ways you can define commands in your ViewModel. One option could be:

public ICommand EntryOkNext
{
    get 
    { 
        return new Command (() => 
        {
            // your code here
        });
    }
}

Upvotes: 1

Related Questions