Esempio n. 1
0
        protected override void OnTextChanged(string oldTextValue, string newTextValue)
        {
            base.OnTextChanged(oldTextValue, newTextValue);

            if (IsSavingEnabled)
            {
                // Cancels the current execution
                if (cancellationTokenSource != null)
                {
                    cancellationTokenSource.Cancel();
                }

                // Creates a new instance for the cancellation token
                IsSaving = true;
                cancellationTokenSource = new CancellationTokenSource();

                // Executes a background operation
                Task.Run(async() =>
                {
                    // Takes the token reference reference that will be cancelled after the next character inserted
                    var token = cancellationTokenSource.Token;

                    // Getting the bytes from the url that was inserted on the text
                    var bytes = await downloadService.DownloadImageUrl(newTextValue);

                    // If the token wasn't cancelled (when another character is inserted), send it to the ViewModel
                    if (!token.IsCancellationRequested)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            SaveCommand?.Execute(bytes);

                            IsSaving = false;
                        });
                    }
                }, cancellationTokenSource.Token);
            }
        }