Ejemplo n.º 1
0
 private void SetBadgeCount()
 {
     badgeLayout.IsVisible = BadgeCount > 0;
     badgeLabel.Text       = BadgeCount > 99 ? "99" : BadgeCount.ToString();
 }
Ejemplo n.º 2
0
        private async Task <string> CreateBadge(CancellationToken token)
        {
            //Get the badge icon
            IImage badgeIcon = null;

            if (_mode == Mode.File)
            {
                badgeIcon = await _canvasService.GetImage(FileIcon, token);
            }
            else if (_mode == Mode.Svg)
            {
                badgeIcon = await _canvasService.GetSvgImage(SvgIcon, SvgWidth, SvgHeight, token);
            }

            if (badgeIcon == null)
            {
                return(null);
            }

            token.ThrowIfCancellationRequested();

            IImageCanvas canvas = null;

            if (IsBadgeVisible)
            {
                //InitializeCache frames
                var font      = new Font(null, BadgeFontSize);
                var metrics   = _canvasService.MeasureText(BadgeCount.ToString(), font);
                var textSize  = metrics.Size;
                var badgeSize = new Size(BadgePadding.Left + BadgePadding.Right + textSize.Width,
                                         BadgePadding.Top + BadgePadding.Bottom + textSize.Height);
                var canvasSize = new Size(badgeIcon.Size.Width + badgeSize.Width - BadgeInsets.Width,
                                          badgeIcon.Size.Height + badgeSize.Height - BadgeInsets.Height);

                //Create canvas
                canvas = _canvasService.GetCanvas(canvasSize);
                token.ThrowIfCancellationRequested();

                //Draw icon
                var imageFrame = new Rect(0, badgeSize.Height - BadgeInsets.Height, badgeIcon.Size.Width,
                                          badgeIcon.Size.Height);
                canvas.DrawImage(badgeIcon, imageFrame);
                token.ThrowIfCancellationRequested();

                //Draw the badge rounded rect
                var badgeFrame = new Rect(badgeIcon.Size.Width - BadgeInsets.Width, 0, badgeSize.Width,
                                          badgeSize.Height);
                var color      = GetColor(BadgeColor);
                var badgeImage = _canvasService.GetRoundedImage(badgeFrame.Size, color, BadgeRadius);
                canvas.DrawImage(badgeImage, badgeFrame);
                token.ThrowIfCancellationRequested();

                //Draw badge text
                var textFrame = new Rect(badgeFrame.X + BadgePadding.Left,
                                         badgeFrame.Y + BadgePadding.Top + metrics.Ascent, textSize.Width, textSize.Height);
                canvas.DrawText(BadgeCount.ToString(), textFrame, font, TextAlignment.Center,
                                GetColor(BadgeTextColor));
            }
            else
            {
                //Create canvas
                canvas = _canvasService.GetCanvas(badgeIcon.Size);
                token.ThrowIfCancellationRequested();

                //Draw icon
                var imageFrame = new Rect(0, 0, badgeIcon.Size.Width, badgeIcon.Size.Height);
                canvas.DrawImage(badgeIcon, imageFrame);
            }

            token.ThrowIfCancellationRequested();

            //Get the result image
            var image = canvas.GetImage();

            //Save the image to cache
            var filename = GetBadgeCacheFileName();
            var path     = PortablePath.Combine(_cacheFolder.Path, filename);
            await _canvasService.SaveImage(image, _cacheFolder.Path, filename, token);

            return(path);
        }