提问人:Kraytonian 提问时间:11/1/2023 最后编辑:Kraytonian 更新时间:11/2/2023 访问量:34
矩形条不显示为不同的颜色或根本不显示
Rectangle Bars are not showing up as different colors or at all
问:
我希望条形图根据评级显示为不同的颜色。我还希望我的转换是否能按预期实际工作,而不是给我一个错误。
我的转换器不工作,我无法看到基于评级绑定的颜色变化?
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>
RatingView模型
命名空间 RatingAgency.ViewModels { 公共类 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;
}
}
}
}
严重性代码说明 项目文件行抑制状态 错误XLS0414找不到类型“local:RatingConverter”。验证是否缺少程序集引用,以及是否已生成所有引用的程序集。MauiService C:\Users\johnd\Documents\MauiService\Views\RatingPage.xaml 12
答: 暂无答案
评论
Rating
EventsViewModel
RatingViewModel