Exemple #1
0
        private async void StartGradientAnimation()
        {
            _cancellationTokenSource = new CancellationTokenSource();

            var gradientStates = new List <(Color, Color)>
            {
                (Color.FromHex("#4A148C"), Color.FromHex("#D500F9")),
                (Color.FromHex("#FF6F00"), Color.FromHex("#FFC400")),
                (Color.FromHex("#1B5E20"), Color.FromHex("#00E676")),
                (Color.FromHex("#B71C1C"), Color.FromHex("#FF1744"))
            };

            var state = 0;

            while (!_cancellationTokenSource.Token.IsCancellationRequested)
            {
                var fromColorStart = gradientStates[state].Item1;
                var fromColorEnd   = gradientStates[state].Item2;

                var nextState    = state + 1 > 3 ? 0 : state + 1;
                var toColorStart = gradientStates[nextState].Item1;
                var toColorEnd   = gradientStates[nextState].Item2;

                var taskCompletionSource = new TaskCompletionSource <bool>();

                new Animation
                {
                    {
                        0, 1, new Animation(d =>
                        {
                            BackgroundEffect.GetBackground(BackgroundView).GradientBrush.Gradients =
                                new List <GradientStop>
                            {
                                new GradientStop(GetColor(d, fromColorStart, toColorStart), 0),
                                new GradientStop(GetColor(d, fromColorEnd, toColorEnd), 1)
                            };
                        })
                    }
                }.Commit(BackgroundView, "name", 16, 4000, Easing.Linear,
                         (d, b) => taskCompletionSource.TrySetResult(true));

                await taskCompletionSource.Task;

                if (_cancellationTokenSource.Token.IsCancellationRequested)
                {
                    break;
                }

                state++;

                if (state > 3)
                {
                    state = 0;
                }
            }
        }
Exemple #2
0
        private void SetVisualElement(VisualElement oldElement, VisualElement newElement)
        {
            if (oldElement != null)
            {
                _context    = null;
                _nativeView = null;
                oldElement.PropertyChanged -= OnElementPropertyChanged;
            }

            if (newElement != null)
            {
                ResolveNativeView();
                _context = _nativeView.Context;
                newElement.PropertyChanged += OnElementPropertyChanged;
            }

            IMaterialVisualElement oldMaterialVisualElement;
            IMaterialVisualElement newMaterialVisualElement;

            switch (oldElement)
            {
            case IBackgroundElement oldBackgroundElement:
                oldMaterialVisualElement = oldBackgroundElement.Background;
                break;

            case IMaterialVisualElement oldMaterialElement:
                oldMaterialVisualElement = oldMaterialElement;
                break;

            default:
                oldMaterialVisualElement = oldElement == null ? null : BackgroundEffect.GetBackground(oldElement);
                break;
            }

            switch (newElement)
            {
            case IBackgroundElement newBackgroundElement:
                newMaterialVisualElement = newBackgroundElement.Background;
                break;

            case IMaterialVisualElement newMaterialElement:
                newMaterialVisualElement = newMaterialElement;
                break;

            default:
                newMaterialVisualElement = newElement == null ? null : BackgroundEffect.GetBackground(newElement);
                break;
            }

            _visualElement = newElement;

            SetBackgroundElement(oldMaterialVisualElement, newMaterialVisualElement);
        }
        private static IMaterialVisualElement GetMaterialVisualElement(VisualElement element)
        {
            switch (element)
            {
            case IBackgroundElement oldBackgroundElement:
                return(oldBackgroundElement.Background);

            case IMaterialVisualElement oldMaterialElement:
                return(oldMaterialElement);

            default:
                return(element == null ? null : BackgroundEffect.GetBackground(element));
            }
        }
        private void SetVisualElement(VisualElement oldElement, VisualElement newElement)
        {
            if (oldElement != null)
            {
                oldElement.PropertyChanged -= OnElementPropertyChanged;
            }

            if (newElement != null)
            {
                newElement.PropertyChanged += OnElementPropertyChanged;
            }

            IMaterialVisualElement oldMaterialVisualElement;
            IMaterialVisualElement newMaterialVisualElement;

            switch (oldElement)
            {
            case IBackgroundElement oldBackgroundElement:
                oldMaterialVisualElement = oldBackgroundElement.Background;
                break;

            case IMaterialVisualElement oldMaterialElement:
                oldMaterialVisualElement = oldMaterialElement;
                break;

            default:
                oldMaterialVisualElement = oldElement == null ? null : BackgroundEffect.GetBackground(oldElement);
                break;
            }

            switch (newElement)
            {
            case IBackgroundElement newBackgroundElement:
                newMaterialVisualElement = newBackgroundElement.Background;
                break;

            case IMaterialVisualElement newMaterialElement:
                newMaterialVisualElement = newMaterialElement;
                break;

            default:
                newMaterialVisualElement = newElement == null ? null : BackgroundEffect.GetBackground(newElement);
                break;
            }

            _visualElement = newElement;

            SetBackgroundElement(oldMaterialVisualElement, newMaterialVisualElement);
        }