Beispiel #1
0
        protected async Task RunMessagePattern(int fadeTimeMillis, int duration, CancellationToken cancellationToken)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            // Cancel previous running.
            AnimateOnMain(() =>
            {
                if (messageBox.AnimationIsRunning("FadeTo"))
                {
                    messageBox.AbortAnimation("FadeTo");
                }
            });

            bool   initial          = true;
            string message          = string.Empty;
            int    durationRequired = 0;

            while (!cancellationToken.IsCancellationRequested) // Keep running pattern until done aerating.
            {
                cancellationToken.ThrowIfCancellationRequested();

                for (int i = 0; i < messagePattern.Count; i++)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (messagePattern[i] == MessageType.FUN)
                    {
                        message = funMessageSource.GetNext();
                    }
                    else if (messagePattern[i] == MessageType.FACT)
                    {
                        message = factMessageSource.GetNext();
                    }

                    if (initial)
                    {
                        durationRequired = EstimatedReadTimeMillis(message) + fadeTimeMillis;
                    }
                    else
                    {
                        durationRequired = EstimatedReadTimeMillis(message) + (fadeTimeMillis * 2);
                    }

                    // Don't show if too close to end, user wont' be able to read text.
                    if (durationRequired <= Math.Abs(duration - sw.ElapsedMilliseconds) + 500)
                    {
                        AnimateOnMain(async() =>
                        {
                            if (!initial) // Don't fade out on first.
                            {
                                if (!messageBox.AnimationIsRunning("FadeTo"))
                                {
                                    await messageBox.FadeTo(0, (uint)fadeTimeMillis);
                                }
                            }
                            initial         = false; // Always fade from now on.
                            messageBox.Text = message;
                            if (!messageBox.AnimationIsRunning("FadeTo"))
                            {
                                await messageBox.FadeTo(1.0d, (uint)fadeTimeMillis);
                            }
                        });
                        await Task.Delay(durationRequired, cancellationToken);
                    }
                    else
                    {
                        await Task.Delay((int)Math.Abs(duration - sw.ElapsedMilliseconds));
                    }
                }
            }
        }