它沒有,則需要使用第三方庫,提供異步命令。我個人喜歡Nito.Mvvm.Async,它給你一個AsyncCommand,你可以使用並綁定你的函數。當異步函數運行時,該按鈕將被禁用,並且一旦該功能完成,該按鈕將重新啓用。
<ContentPage.ToolbarItems>
<ToolbarItem Text = "Done" Command="{Binding DoneCommand}" />
<ToolbarItem Text = "Cancel" Command="{Binding CancelCommand}" Priority="1" />
</ContentPage.ToolbarItems>
在您的視圖moodel。
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>
在您的視圖moodel。
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();
}
那麼,它被稱爲同步? –
這應該給你一些看法:https://stackoverflow.com/questions/37419572/if-async-await-doesnt-create-any-additional-threads-then-how-does-it-make-appl – Thowk
我不知道看不到相關性。 –