I use ShowLoading() Acr UserDialogs (v5.2.2) on my xamarin forms project (android and ios) but i wont start loader before start await method and Hide Loader with the end.
My code working on ios but on android nothing happened.
example
async Task MyMethod()
{
UserDialogs.Instance.ShowLoading("Loading",MaskType.Black);
await ViewModel.LoadData();
UserDialogs.Instance.HideLoading();
}
//InsideViewModel
public async Task LoadData();
{
await Task.Yield(); //without this code and ios doesnt work
//download data
}
I add for android UserDialogs.Init(this) on MainActivity.cs
Any help please ? Thanks
I write some code where is really fast and working perfect with Acr User dialogs.
I use depedency service and here is my full sample code.
PCL code
void CallService ()
{
Device.BeginInvokeOnMainThread (() => UserDialogs.Instance.ShowLoading ("Loading ...", MaskType.Black));
Task.Run (async () => {
await DependencyService.Get<IJsonMethods> ().Load ("SERVICE_URL");
}).ContinueWith (result => Device.BeginInvokeOnMainThread (() => {
UserDialogs.Instance.HideLoading ();
}
})
);
}
Interface
public interface IJsonMethods
{
Task Load(string url);
}
Dependency Service
public async Task Load (string url)
{
try{
using (var http = new HttpClient (new NativeMessageHandler ())) {
var x = await http.GetAsync (new Uri (url));
string res = await x.Content.ReadAsStringAsync ();
MainViewModel.JsonList = JsonConvert.DeserializeObject<ArticleClass.RootObject> (res);
}
}catch (Exception ex) {
MainViewModel.JsonList = null;
}
}
I use a more elegant way:
async Task MyMethod()
{
using(UserDialogs.Instance.ShowLoading("Loading",MaskType.Black))
{
await ViewModel.LoadData();
}
}
//InsideViewModel
public async Task LoadData();
{
await Task.Yield(); //without this code and ios doesnt work
//download data
}
You should't need to use the await Task.Yield();
.
Please try this:
async Task MyMethod()
{
UserDialogs.Instance.ShowLoading("Loading", MaskType.Black);
await ViewModel.LoadData();
UserDialogs.Instance.HideLoading();
}
//InsideViewModel
public async Task LoadData()
{
await DownloadFileAsync("http://url.to.some/file.mp3", "file.mp3");
}
public async Task DownloadFileAsync(string url, string filename)
{
var destination = Path.Combine(
System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ApplicationData),
filename);
await new WebClient().DownloadFileTaskAsync(new Uri(url), destination);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With