Reputation: 1
I want the bars to show as a different color based on the rating. I would also like if my convert would actual work like it is intended instead of giving me an error.
My converter is not working I am not able to see the color change based of the Rating Binding?
RatingPage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RatingAgency.Views.EventsPage"
xmlns:local="clr-namespace:RatingAgency.ViewModels"
BackgroundColor="DarkBlue">
<ContentPage.BindingContext>
<local:RatingViewModel />
</ContentPage.BindingContext>
<ContentPage.Resources>
<local:RatingConverter x:Key="RatingColorChange" />
</ContentPage.Resources>
<StackLayout Style="{StaticResource Bars}">
<Rectangle VerticalOptions="End" Margin="2,0,0,0" WidthRequest="5" HeightRequest="5" Fill="{Binding Rating, Converter={StaticResource RatingColorChange}}"/>
<Rectangle VerticalOptions="End" Margin="2,0,0,0" WidthRequest="5" HeightRequest="10" Fill="{Binding Rating, Converter={StaticResource RatingColorChange}}"/>
<Rectangle VerticalOptions="End" Margin="2,0,0,0" WidthRequest="5" HeightRequest="15" Fill="{Binding Rating, Converter={StaticResource RatingColorChange}}"/>
</StackLayout>
</ContentPage>
RatingViewModel
namespace RatingAgency.ViewModels { public class RatingViewModel: ViewModelBase {
public List<Event> Events
{
get { return events; }
set { events = value; OnPropertyChanged(); }
}
public ICommand ButtonClickedCommand { get; }
/// <summary>
/// Constructor
/// </summary>
public RatingViewModel()
{
}
/// <summary>
/// Rating Convert to color
/// </summary>
public class RatingConverter : IValueConverter, IMarkupExtension
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
double rate;
double.TryParse(value.ToString(), out rate);
if (rate >= 66)
{
return Brush.Green;
}
else if (rate >= 60)
{
return Brush.Green;
}
else if (rate >= 54.5)
{
return Brush.Orange;
}
else if (rate <= 54.3)
{
return Brush.Red;
}
}
return Brush.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
}
Severity Code Description Project File Line Suppression State Error XLS0414 The type 'local:RatingConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. MauiService C:\Users\johnd\Documents\MauiService\Views\RatingPage.xaml 12
Upvotes: 0
Views: 35