/// <summary>
        /// Loads and gets BitmapDrawable using defined parameters.
        /// IMPORTANT: you should call SetNoLongerDisplayed method if drawable is no longer displayed
        /// IMPORTANT: It throws image loading exceptions - you should handle them
        /// </summary>
        /// <returns>The bitmap drawable async.</returns>
        /// <param name="parameters">Parameters.</param>
        public static Task <SelfDisposingBitmapDrawable> AsBitmapDrawableAsync(this TaskParameter parameters)
        {
            var target            = new BitmapTarget();
            var userErrorCallback = parameters.OnError;
            var finishCallback    = parameters.OnFinish;
            var tcs = new TaskCompletionSource <SelfDisposingBitmapDrawable>();

            parameters
            .Error(ex =>
            {
                tcs.TrySetException(ex);
                userErrorCallback?.Invoke(ex);
            })
            .Finish(scheduledWork =>
            {
                finishCallback?.Invoke(scheduledWork);
                tcs.TrySetResult(target.BitmapDrawable);
            });

            if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path))
            {
                target.SetAsEmpty(null);
                parameters.TryDispose();
                return(null);
            }

            var task = ImageService.CreateTask(parameters, target);

            ImageService.Instance.LoadImage(task);

            return(tcs.Task);
        }
Esempio n. 2
0
        public static Task <WriteableBitmap> AsWriteableBitmapAsync(this TaskParameter parameters)
        {
            BitmapTarget            target             = new BitmapTarget();
            Action <Exception>      userErrorCallback  = parameters.OnError;
            Action <IScheduledWork> finishCallback     = parameters.OnFinish;
            TaskCompletionSource <WriteableBitmap> tcs = new TaskCompletionSource <WriteableBitmap>();

            parameters.Error(delegate(Exception ex)
            {
                tcs.TrySetException(ex);
                userErrorCallback?.Invoke(ex);
            }).Finish(delegate(IScheduledWork scheduledWork)
            {
                finishCallback?.Invoke(scheduledWork);
                tcs.TrySetResult(target.BitmapSource as WriteableBitmap);
            });
            if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path))
            {
                target.SetAsEmpty(null);
                parameters.TryDispose();
                return(null);
            }
            IImageLoaderTask task = ImageService.CreateTask(parameters, target);

            ImageService.Instance.LoadImage(task);
            return(tcs.Task);
        }