我希望只有在async
方法返回后才执行Method2。
有没有办法做到这一点 ?
public class ViewModelBase
{
public ICommand ClickCommand {get; private set;}
public ViewModelBase()
{
ClickCommand= new DelegateCommand<object>(myAsyncMethod);
}
private async void myAsyncMethod(object Param)
{
await Task.Run(()=>
{
// Do stuff
});
}
private void Method2()
{
//Do things
}
}
<ig:XamBusyIndicator DataContext="{Binding vm}" IsBusy="{Binding IsRunning}">
<ContentControl Content="{Binding DisplayType}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate></DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Content"
Value="One">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<igDP:XamDataGrid
DataContext="{Binding DataContext}"
DataSource="{Binding UnDustedCollection}"></igDP:XamDataGrid>
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="Content"
Value="Two">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<igDP:XamDataGrid
DataContext="{Binding DataContext}"
DataSource="{Binding DustedCollection}"></igDP:XamDataGrid>
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</ig:XamBusyIndicator>
答案 0 :(得分:0)
只需在await
之后调用您的方法或在另一个异步方法中包装myAsyncMethod
,并在那里进行类似的操作。
private async void myAsyncMethod(object Param)
{
await Task.Run(()=>
{
// Do stuff. Not going to have safe access to UI elements
});
// await completed asynchronously.
Method2(); // this method will have safe access to UI elements
}
请注意,如果您需要与按钮状态更紧密地集成 - How does a toolbar button know to await?有一些想法(由Scott Chamberlain撰写suggested)。