没有,您需要使用提供异步命令的 3rd 方库。我个人喜欢Nito.Mvvm.Async,它为您提供了一个 AsyncCommand,您可以使用和绑定您的函数。该按钮将在异步功能运行时被禁用,并在功能完成后重新启用。
<ContentPage.ToolbarItems>
<ToolbarItem Text = "Done" Command="{Binding DoneCommand}" />
<ToolbarItem Text = "Cancel" Command="{Binding CancelCommand}" Priority="1" />
</ContentPage.ToolbarItems>
在你看来穆德尔。
public MyViewModel()
{
CancelCommand = new AsyncCommand(ExecuteCancel);
}
public AsyncCommand CancelCommand {get;}
async Task ExecuteCancel()
{
await Navigation.PopModalAsync();
}
这是一个更复杂的版本,它禁用取消选项,除非“完成”选项当前正在运行。
<ContentPage.ToolbarItems>
<ToolbarItem Text = "Done" Command="{Binding DoneCommand}" />
<ToolbarItem Text = "Cancel" Command="{Binding CancelCommand}" Priority="1" />
</ContentPage.ToolbarItems>
在你看来穆德尔。
public MyViewModel()
{
DoneCommand = new AsyncCommand(ExecuteDone);
CancelCommand = new CustomAsyncCommand(ExecuteCancel, CanExecuteCancel);
PropertyChangedEventManager.AddHandler(DoneCommand, (sender, e) => CancelCommand.OnCanExecuteChanged(), nameof(DoneCommand.IsExecuting));
PropertyChangedEventManager.AddHandler(CancelCommand, (sender, e) => CancelCommand.OnCanExecuteChanged(), nameof(CancelCommand.IsExecuting));
}
private bool CanExecuteCancel()
{
return DoneCommand.IsExecuting && !CancelCommand.IsExecuting;
}
public AsyncCommand DoneCommand { get; }
public CustomAsyncCommand CancelCommand { get; }
async Task ExecuteDone()
{
await ... //Do stuff
}
async Task ExecuteCancel()
{
await Navigation.PopModalAsync();
}