I'm working on a Xamarin Forms MvvmCross project. I'm binding a common button command on the view with a IMvxAsyncCommand on the view model, as follows:
View
<views:MvxContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:views="clr- namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms" x:Class="TestProject.Pages.TestPage"> <ContentView> <StackLayout> <Button Text="Test the command!" Command="{Binding TestAsyncCommand}"/> </StackLayout> </ContentView>
</views:MvxContentPage>
View model
namespace TestProject.ViewModels { public class TestViewModel : MvxNavigationViewModel { public TestViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService) : base(logProvider, navigation) { } public IMvxAsyncCommand TestAsyncCommand => new MvxAsyncCommand(async () => await TestAsyncCommandMethod()); private async Task TestAsyncCommandMethod() { //await some stuff } } }
Anythings seems to work fine but rarely sometimes after a pression of the button it becames disable. What could happen? It does not happen if I use Command in place of MvxAsyncCommand, like here:
public Command TestAsyncCommand => new Command(async () => await TestAsyncCommandMethod());
Note: TestAsyncCommandMethod is entirely encased in a try-catch block to avoid possible exceptions.
TestAsyncCommandMethod()
look like? – JKLCommand
is from native Xamarin.Forms,butIMvxAsyncCommand
is fromMvvmCross
.By the way, Suggest that usingpublic IMvxAsyncCommand TestAsyncCommand { get; private set; }
. – Junior Jiang