【发布时间】:2016-05-27 21:40:55
【问题描述】:
我正在使用异步,等待在按钮单击时更新 UI,以指示用户正在进行一些进度,并且我正在显示加载面板 onclick of button。以下是我的代码:
安卓:
async void loginButtonGesture_Tapped(object sender, EventArgs e)
{
try
{
DependencyService.Get<Idevice>().StartLoading();
await AsyncUpdateStatus();
}
}
private async Task AsyncUpdateStatus()
{
try
{
await Task.Run(() =>
{
//Device.BeginInvokeOnMainThread is used in places to stop loading the screen
});
}
}
iOS:
async void loginButtonGesture_Tapped(object sender, EventArgs e)
{
try
{
DependencyService.Get<Idevice>().StartLoading();
await Task.Delay(40); //Required for iOS, otherwise loading panel is keep on running
await AsyncUpdateStatus();
}
}
private async Task AsyncUpdateStatus()
{
try
{
//await Task.Run(() => Enabling new thread also not working
//Device.BeginInvokeOnMainThread is used in places to stop loading the screen
}
}
Android 和 iOS 共享相同的表单代码。但是对于这种特殊情况,它有条件地工作。 iOS 在添加Task.Delay(40) 和评论Task.Run(() => {}); 线程后工作。
为什么会这样?但是,如果我在 Android 中进行上述修改,它就不起作用。到底发生了什么?
【问题讨论】:
标签: c# async-await xamarin.forms