Search code examples
c#xamarinxamarin.formsxamarin.android

Loading collection view with task async method


I am trying to load thumbnails with async task method with depency service :

In my pcl page I have this :

protected override void OnAppearing()
        {

            Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.ShowLoading("Loading...", MaskType.Black));

            Task.Run(async () =>
            {
                directoryPath = await getThumbnails.GetBitmaps(fileInfo.FullName);
                List<ThumbnailsModel> thumbnailsModels = new List<ThumbnailsModel>();

                int i = 1;
                Directory.GetFiles(directoryPath).ToList<string>().ForEach(delegate (string thumbnailsEmplacement)
                {
                    thumbnailsModels.Add(new ThumbnailsModel(i, thumbnailsEmplacement));
                    i++;
                });
                CollectionViewThumbnails.ItemsSource = thumbnailsModels;


            }).ContinueWith(result => Device.BeginInvokeOnMainThread(() =>
            {


                UserDialogs.Instance.HideLoading();
            }
        )
        );
        }

My method to get the thumbnails :

public async Task<string> GetBitmaps(string filePath)
    {

        //TODO-- WORK ON THIS

        var appDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
        string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
        string directoryPath = System.IO.Path.Combine(appDirectory, "thumbnailsTemp", System.IO.Path.GetFileNameWithoutExtension(fileName));

        var stream = new MemoryStream();

        using (Stream resourceStream = new FileStream(filePath, FileMode.Open))
        {
            resourceStream.CopyTo(stream);
        }

        Document document = new Document(stream);
        int count = document.Pages.Count;

        for(int i = 0; i<= count; i++) {
            TallComponents.PDF.Rasterizer.Page page = document.Pages[0];

            using (var outputStream = new FileStream(System.IO.Path.Combine(directoryPath, fileName + "Thumbnails" + i + ".png"), FileMode.Create, FileAccess.Write))
            {

                await Task.Run(() =>
                {

                    page.SaveAsBitmap(outputStream, CompressFormat.Png, 5);
                });
            }
        }

        return directoryPath;
    }

The problem is that my application is going in my Dependency service method then going back in my pcl OnAppearing method before the thumbnails are done and going at this line

UserDialogs.Instance.HideLoading();


Solution

  • Seems like you have an unhandled exception. That continuation will run even if an exception is thrown on the Task you're continuing.

    This can be changed using something like TaskContinuationOptions.OnlyOnRanToCompleted (and others) in the overload for ContinueWith. The default is TaskContinuationOptions.None if not specified.

    Alternatively, you can access result.Exception in your continuation if you want it to run on failure and handle it.