Issue
Tools: I am using Xamarin.form MVVM.
Question: How do I set up basic AsyncCommand command with Task?
Debug: I am not getting any error, when I click on button, Displaybox alert doesnt show up. I check and addcommand method is not getting runned on tap.
Bind: I also have bind View with ViewModel
BindingContext = new ProductViewModel();
I watched a tutorial by microsoft and there seem to be working just fine. Did I missed something?
View Page
<Button Text="Add" Command="{Binding AddCommand}" />
ViewModel Page
public class ProductViewModel : INotifyPropertyChanged
{
...
public AsyncCommand AddComamnd { get; }
public ProductViewModel()
{
...
AddComamnd = new AsyncCommand(OnAddComamnd);
}
public async Task OnAddComamnd()
{
await App.Current.MainPage.DisplayAlert("Alert", "You have been alerted", "OK");
// I have also tried both (App and Application) neither works
}
}
Solution
ok, first of all there's a lot of typo, you put AddCommand here:
<Button Text="Add" Command="{Binding AddCommand}" />
So you need same command name:
public Command AddCommand { get; }
Then:
public ProductViewModel()
{
...
AddCommand = new Command(async () => await OnAddComamnd()); //OnAddCommamnd is your async task
}
you can watch about command in here: Episode 5: MVVM & Data Binding with Xamarin.Forms start at minute 22
Answered By - Godya Aditya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.