【发布时间】:2018-08-03 18:00:34
【问题描述】:
我有这个在按下按钮时调用的代码:
async void OnTapped(object sender, System.EventArgs e)
{
var btn = sender as ButtonTemplate;
if (btn == null)
return;
if (btn.Text == Settings.cc.ShortText())
return;
if (Counts.phaseTableSelectedCardCount != 0)
{
var canContinue = await this.DisplayAlert("Selector", "Changing this will remove all previously selected cards from the deck", "OK", "Cancel");
if (canContinue == false)
return;
}
var settingId = SetButtons(btn.Text);
//
// These updates take 1-3 seconds
//
App.DB.UpdateSelected(false);
App.DB.UpdateIntSetting(SET.Cc, settingId);
AddDetailSection();
}
public void AddDetailSection()
{
vm.IsBusy = true;
Change.cardSelection = true;
App.DB.GetData();
detailsLayout.Children.Clear();
detailsLayout.Children.Add(CreateDetailSection(null));
SetPageDetails();
vm.IsBusy = false;
}
按下按钮后,SetButtons(btn.Text); 代码会更改按钮颜色。但是,直到 2-3 秒后,当这些行执行后,我才看到对 UI 的影响,
App.DB.UpdateSelected(false);
App.DB.UpdateIntSetting(SET.Cc, settingId);
AddDetailSection();
有没有办法可以改变UI颜色,让上面三行可以在后台运行?
【问题讨论】:
-
考虑使用
asyncawait模式 -
我想使用该模式,但我不确定在这种情况下如何使用它,因为 onTapped 已经是异步的。你能举个例子说明你使用我的代码的意思吗?
-
简短回答你不要,你在你的
App.DB.GetData();,await中使用它并使用任何数据库调用的Async版本,返回一个Task和@ 987654330@关键字等等。这将释放 UI 线程,以便您可以看到 UI 更改 -
请注意,它只是 AddDetailSection();需要很长时间的电话。另外两个更新很快,我不关心它们
标签: c# xamarin xamarin.forms