Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

I can't to do work the badge in my App

$
0
0

I have this

public Badge(double size, double fontSize)
        {
            HeightRequest = App.ScreenHeight;
            WidthRequest = HeightRequest;

            //HeightRequest = size;
            //WidthRequest = HeightRequest;

            // Box
            Box = new RoundedBox
            {
                CornerRadius = HeightRequest / 2
            };
            Box.SetBinding(BackgroundColorProperty, new Binding("BoxColor", source: this));
            Children.Add(Box, new Rectangle(0, 0, 1.0, 1.0), AbsoluteLayoutFlags.All);

            // Label
            Label = new Label
            {
                TextColor = Color.White,
                FontSize = fontSize,
                XAlign = TextAlignment.Center,
                YAlign = TextAlignment.Center
            };
            Label.SetBinding(Label.TextProperty, new Binding("Text",
                BindingMode.OneWay, source: this));
            Children.Add(Label, new Rectangle(0, 0, 1.0, 1.0), AbsoluteLayoutFlags.All);

            // Auto-width
            SetBinding(WidthRequestProperty, new Binding("Text", BindingMode.OneWay,
                new BadgeWidthConverter(WidthRequest), source: this));

            // Hide if no value
            SetBinding(IsVisibleProperty, new Binding("Text", BindingMode.OneWay,
                new BadgeVisibleValueConverter(), source: this));

            // Default color
            BoxColor = Color.Red;
        }
    }

    /// <summary>
    /// Badge visible value converter.
    /// </summary>
    class BadgeVisibleValueConverter : IValueConverter
    {
        #region IValueConverter implementation

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var text = value as string;
            return !String.IsNullOrEmpty(text);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    /// <summary>
    /// Badge width converter.
    /// </summary>
    class BadgeWidthConverter : IValueConverter
    {
        /// <summary>
        /// The width of the base.
        /// </summary>
        readonly double baseWidth;

        /// <summary>
        /// The width ratio.
        /// </summary>
        const double widthRatio = 0.33;

        public BadgeWidthConverter(double baseWidth)
        {
            this.baseWidth = baseWidth;
        }

        #region IValueConverter implementation

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var text = value as string;
            if ((text != null) && (text.Length > 1))
            {
                // We won't measure text length exactly here!
                // May be we should, but it's too tricky. So,
                // we'll just approximate new badge width as
                // linear function from text legth.

                return baseWidth * (1 + widthRatio * (text.Length - 1));
            }
            return baseWidth;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

and other class and one renderer in the droid project. I need to show badge in my App. I tried this

<Image x:Name="imgDesvioFat" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Source="{local:ImageResource Operacional.Images.faturamento caixa-28.png}" Aspect="AspectFill">
                        <Image.GestureRecognizers>
                            <TapGestureRecognizer Tapped="OnDesvioFaturamentoTapReconizerTapped" NumberOfTapsRequired="1"></TapGestureRecognizer>
                        </Image.GestureRecognizers>
                    </Image>
                <Badge Grid.Row="0" Grid.Column="0" Grid.RowSpan="2"
                        x:Name="bdgDesvioFat" 
                     BadgeColor="Red"/>    

and in the method where i fill a label i changed to badge and i did this way

..................
 if (desvio < 1000)
                            resultado = Math.Truncate(desvio).ToString();
                        else
                            while (desvio >= 1000 && siglas.Count > 0)
                            {
                                desvio /= 1000;
                                resultado = Math.Truncate(desvio) + siglas[0];
                                siglas.RemoveAt(0);
                            }

                        if (CorIndicador == "VERDE")
                            image.Source = ImageSource.FromResource("Estapar.AppOperacional.Images.faturamento caixa-28.png");
                        else
                        {
                            var pinta = CorIndicador == "VERMELHO" ? image.Source = ImageSource.FromResource("Estapar.AppOperacional.Images.faturamento caixa-26.png") : image.Source = ImageSource.FromResource("Estapar.AppOperacional.Images.faturamento caixa-27.png");
                        }              

                        //label.Text = resultado;
            bdgDesvioFat.Text = resultado;

but i'm getting an error in the NameSpace.views.xaml.g.cs

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.Badge bdgDesvioFat;

the error is:

the type name or namespace "Badge" doesn't exist in the namespace "Xamarin.Forms"

what am i wrong doing? How can i solve this?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>