![](/img/trans.png)
[英]Xamarin.Forms - Slider not updating on property assignment in iOS, works in Android
[英]Xamarin.Forms updating UI thread behaving strangely in iOS & Android
我正在使用异步,等待按钮点击更新UI,以指示用户正在进行一些进展,我正在显示加载面板上的按钮。 以下是我的代码:
安卓:
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都共享相同的表单代码。 但对于这个特殊情况,它有条件地工作。 添加Task.Delay(40)
并评论Task.Run(() => {});
后iOS正在运行Task.Run(() => {});
线。
为什么会这样? 但是,如果我在Android中进行上述修改,则无法正常工作。 究竟发生了什么?
不知怎的,你阻止了UIThread,关于Xamarin论坛有很多问题:
https://forums.xamarin.com/discussion/47019/activity-indicator-inconsistencies
https://forums.xamarin.com/discussion/54237/update-uitextview-text-in-code
https://forums.xamarin.com/discussion/41229/best-practise-waiting-and-synchronising-threads
似乎你在你的View类中这样做,你应该在你的ViewModel中做这个,根据经验,我鼓励你不要等待任务,除非是完全必要的,否则你应该使用Bindings让用户有事情发生上!
大多数人都是亚当回答的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.