Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Acr User Dialogs loader until end of method xamarin forms (android)

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

like image 416
G.Mich Avatar asked Jun 15 '16 10:06

G.Mich


3 Answers

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;
        }
    }
like image 90
G.Mich Avatar answered Nov 18 '22 16:11

G.Mich


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
}
like image 21
MoreiraWebMaster Avatar answered Nov 18 '22 16:11

MoreiraWebMaster


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);
} 
like image 2
jzeferino Avatar answered Nov 18 '22 17:11

jzeferino