C# 表单更新UI线程在iOS&;安卓
我正在使用async,等待在按钮单击时更新UI,以指示用户正在进行某些操作,我在单击按钮时显示加载面板。下面是我的代码: Android:C# 表单更新UI线程在iOS&;安卓,c#,async-await,xamarin.forms,C#,Async Await,Xamarin.forms,我正在使用async,等待在按钮单击时更新UI,以指示用户正在进行某些操作,我在单击按钮时显示加载面板。下面是我的代码: Android: async void loginButtonGesture_Tapped(object sender, EventArgs e) { try { DependencyService.Get<Idevice>().StartLoading(); await AsyncUpdateSta
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
});
}
}
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
}
}
async void loginButtonGesture_(对象发送方,事件参数e)
{
尝试
{
DependencyService.Get().starting();
等待AsyncUpdateStatus();
}
}
专用异步任务AsyncUpdateStatus()
{
尝试
{
等待任务。运行(()=>
{
//Device.BeginInvokeMainThread用于停止加载屏幕
});
}
}
iOS:
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
});
}
}
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
}
}
async void loginButtonGesture_(对象发送方,事件参数e)
{
尝试
{
DependencyService.Get().starting();
等待任务。延迟(40);//iOS需要,否则加载面板将继续运行
等待AsyncUpdateStatus();
}
}
专用异步任务AsyncUpdateStatus()
{
尝试
{
//等待任务。运行(()=>启用新线程也不工作
//Device.BeginInvokeMainThread用于停止加载屏幕
}
}
Android和iOS都共享相同的表单代码。但对于这种特殊情况,它是有条件地工作的。iOS在添加Task.Delay(40)
和注释Task.Run(()=>{});
thread后工作
为什么会这样?但如果我在Android中进行上述修改,它就不起作用了。到底发生了什么事?不知为什么你阻止了UIThread,Xamarin论坛上有很多关于这方面的问题: 看起来你是在视图类中这样做的,你应该在ViewModel中这样做,根据经验,我鼓励你不要等待任务,除非是完全必要的,否则你应该使用绑定让用户知道正在发生什么
大多数问题的答案都是Adam不知何故,你阻止了UIThread,Xamarin论坛上有很多关于这方面的问题: 看起来你是在视图类中这样做的,你应该在ViewModel中这样做,根据经验,我鼓励你不要等待任务,除非是完全必要的,否则你应该使用绑定让用户知道正在发生什么 他们大多数人的回答是亚当