在我的xamarin.forms应用程序中,我使用 Acr.Userdialogs 来显示加载指示符。我试图做的是单击一个按钮时将执行一个同步方法。 api将调用并根据api结果从该页面进行导航。api调用和导航工作正常。但是我无法使用Acr.user对话框显示加载指示器。应该是什么问题?我应该在 Device.BeginInvokeOnMainThread 上运行整个方法吗?请指教。
我的按钮点击
private void submit_clicked(object sender, EventArgs args)
{
UserDialogs.Instance.ShowLoading("Submitting data...");
SubmitData();
}
private void SubmitData()
{
if (userMappedStateListData.Entry[0].State.Contains(State_picker.Text))
{
try
{
//<=================My API CALL =======================>
............................................
............................................
............................................
// Result of Api is placed in a list called transactions
if (transactions[0].Questions.Count < 5)
{
UserDialogs.Instance.HideLoading();
PopupNavigation.PushAsync(new NoDataPopUp());
}
else
{
foreach (Question ques in transactions[0].Questions)
{
foreach (Answer ans in ques.Answers)
{
if (ans.Text != CommonValues.none && ans.Correct == true)
{
ObjQuestionData.Add(ques);
truequescount = ObjQuestionData.Count.ToString();
}
}
}
if (ObjQuestionData.Count < 1)
{
UserDialogs.Instance.HideLoading();
PopupNavigation.PushAsync(new InsuffQdata());
}
else
{
foreach (Question ques in transactions[0].Questions)
{
if (ObjQuestionData.Count < 5)
{
foreach (Answer ans in ques.Answers)
{
if (ans.Text == CommonValues.none && ans.Correct == true)
{
ObjQuestionData.Add(ques);
}
}
}
}
}
}
Tranzaction tranzaction = new Tranzaction()
{
TransactionId = transactions[0].TransactionId,
Questions = ObjQuestionData.GetRange(0, 5)
};
List<String> formdata = new List<String>() { first_name.Text, last_name.Text, street_name.Text, State_picker.Text, zip_code.Text, SSN.Text, BAN.Text, truequescount };
UserDialogs.Instance.HideLoading();
Navigation.PushAsync(new SecurityQuestionScreen(tranzaction, formdata));
}
catch (Exception)
{
}
}
else
{
try
{
List<String> formdata = new List<String>() { first_name.Text, last_name.Text, street_name.Text, State_picker.Text, zip_code.Text, SSN.Text, BAN.Text };
UserDialogs.Instance.HideLoading();
PopupNavigation.PushAsync(new FormVerificationPopup(formdata));
}
catch (Exception ex)
{
}
}
}
答案 0 :(得分:1)
如果您在线程中具有UI交互,则可以通过Device.BeginInvokeOnMainThread
来实现。
private void Button_Clicked(object sender, EventArgs e)
{
UserDialogs.Instance.ShowLoading("Submitting data...");
Task.Run(async () =>
{
// Run code here
await Task.Delay(4000);
Device.BeginInvokeOnMainThread(() =>
{
// UI interaction goes here
UserDialogs.Instance.HideLoading();
});
});
}
有一个相关的博客。 http://lukealderton.com/blog/posts/2016/october/xamarin-forms-working-with-threads/
上传数据
这里是在调用api之后添加一个导航页面。