Mik*_*ish 5 c# portable-class-library xamarin xamarin.forms
我试图显示ActivityIndicator在我尝试更新DataBase上的列字段时按下按钮后,它没有出现?问题是什么?
在以下我的代码:
ActivityIndicator ai = new ActivityIndicator()
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
Color = Color.Black
};
ai.IsRunning = true;
ai.IsEnabled = true;
ai.BindingContext = this;
ai.SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy");
ProcessToCheckOut = new Button { Text = "Set Inf" };
ProcessToCheckOut.Clicked += (object sender, EventArgs e) =>
{
this.IsBusy = true;
UserUpdateRequest user=new UserUpdateRequest();
user.userId = CustomersPage.ID;
appClient.UpdateInfo(user);
this.IsBusy = false;
Navigation.PushAsync(new CheckoutShippingAddressPage(appClient));
};
Content = new StackLayout
{
Children={
tb,
ai,
ProcessToCheckOut
}
};
Run Code Online (Sandbox Code Playgroud)
Kei*_*ome 10
之间的代码中没有一个this.IsBusy=true;与this.IsBusy=false;是异步的.所以发生的事情是你启用指标,然后继续在主线程上工作,然后在UI有机会更新之前禁用指标.
要解决此问题,您需要放入appClient.UpdateInfo(user)异步代码块(以及PushAsync和禁用活动指示器以及可能的其他一些代码).如果您没有异步版本,UpdateInfo()那么您可以将其推送到后台线程...假设它所做的任何工作实际上对于在后台线程中运行是安全的.
ProcessToCheckOut.Clicked += (object sender, EventArgs e) =>
{
this.IsBusy = true;
var id = CustomersPage.ID;
Task.Run(() => {
UserUpdateRequest user=new UserUpdateRequest();
user.userId = id;
appClient.UpdateInfo(user);
Device.BeginInvokeOnMainThread(() => {
this.IsBusy = false;
Navigation.PushAsync(new CheckoutShippingAddressPage(appClient));
});
});
};
Run Code Online (Sandbox Code Playgroud)
请注意,Device.BeginInvokeOnMainThread()在完成后台工作后,我还习惯将执行重新编写回主线程.这并不总是必要的,但这是一种很好的做法.
| 归档时间: |
|
| 查看次数: |
5399 次 |
| 最近记录: |