我在理解.NET MAUI中绑定工作方式方面遇到了问题。我的目标是在CollectionView内有两种显示分数的方式,一种是带数字的标签,另一种是图形化表示。分数与标签的绑定正常工作,但是与Drawable的绑定却不起作用。如果我写一个数字而不是使用绑定,它可以正常传递。
我做错了什么?
ProceduresPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:MediSkillApp.Model"
xmlns:drawables="clr-namespace:MediSkillApp.Drawables"
xmlns:viewmodel="clr-namespace:MediSkillApp.ViewModel"
x:Class="MediSkillApp.View.ProceduresPage"
x:DataType="viewmodel:ProceduresViewModel"
Title="Alle mine indgreb">
<Grid ColumnDefinitions="*"
ColumnSpacing="5"
RowSpacing="0">
<CollectionView
BackgroundColor="Transparent"
ItemsSource="{Binding Procedures}"
RemainingItemsThresholdReachedCommand="{Binding GetProceduresCommand}"
RemainingItemsThreshold="5">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Procedure">
<VerticalStackLayout>
<Frame Margin="5">
<Grid ColumnDefinitions="64,*, 64">
<Image
Grid.Column="0"
Source="{Binding Icon}"
HeightRequest="48"
WidthRequest="48"
HorizontalOptions="Start"/>
<VerticalStackLayout
Grid.Column="1">
<Label Text="{Binding ProcedureTypeString}"
Style="{StaticResource Heading}"/>
<Label Text="{Binding OpRoleString}"
Style="{StaticResource NormalLabel}"/>
<Label Text="{Binding Date, StringFormat='{0:dd/MM-yyyy}'}"
Style="{StaticResource NormalLabel}"/>
</VerticalStackLayout>
<VerticalStackLayout
Grid.Column="2"
IsVisible="{Binding IsScored}">
<!-- this binding works -->
<Label Text="{Binding AvgScore, StringFormat='{0:F2}'}"
HorizontalOptions="Center"/>
<Image Source="scoremeter.png"/>
<GraphicsView>
<GraphicsView.Drawable>
<!-- this binding does not -->
<drawables:ScoreGaugeDrawable
Score="{Binding AvgScore}"/>
</GraphicsView.Drawable>
</GraphicsView>
</VerticalStackLayout>
</Grid>
</Frame>
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<ActivityIndicator IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}"
HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand"/>
</Grid>
</ContentPage>
ScoreGaugeDrawable.cs
namespace MediSkillApp.Drawables;
public class ScoreGaugeDrawable : BindableObject, IDrawable
{
public static readonly BindableProperty ScoreProperty = BindableProperty.Create(nameof(Score),
typeof(double),
typeof(ScoreGaugeDrawable));
public double Score {
get => (double)GetValue(ScoreProperty);
set => SetValue(ScoreProperty, value);
}
public void Draw(ICanvas canvas, RectF dirtyRect)
{
var centerPoint = new PointF(32, 0);
var circleRadius = 5;
canvas.FillColor = Colors.Black;
canvas.FillCircle(centerPoint, circleRadius);
canvas.StrokeColor = Colors.Black;
canvas.DrawLine(centerPoint, new Point(0, Score * 10)); //Just draw something for testing
}
}
Procedure.cs
namespace MediSkillApp.Model;
public class Procedure
{
public string Identifier { get; set; }
public DateTime Date { get; set; }
public string GetDate {
get => Date.ToString("d/M-yyyy");
}
public int ProcedureType { get; set; }
public string ProcedureTypeString { get; set; }
public double AvgScore { get; set; }
public string GetAvgScore {
get {
if (AvgScore == 0) return "";
return AvgScore.ToString();
}
}
public int OpRole { get; set; }
public string OpRoleString { get; set; }
public string Icon {
get {
switch (ProcedureType) {
case 7:
return Icons.IconBleed;
case 8:
return Icons.IconTEA;
case 18:
return Icons.IconTEA;
default:
return Icons.IconSurgery;
}
}
}
public bool IsScored => AvgScore > 0;
}
ProceduresViewModel.cs
using MediSkillApp.Model;
using MediSkillApp.Services;
namespace MediSkillApp.ViewModel;
public partial class ProceduresViewModel : BaseViewModel
{
public ObservableCollection<Procedure> Procedures { get; } = new();
private APIService APIservice;
public ProceduresViewModel(APIService aPIservice) {
APIservice = aPIservice;
}
[RelayCommand]
public async Task GetProceduresAsync() {
if(IsBusy) return;
try {
IsBusy = true;
var procedures = await APIservice.GetProceduresAsync("8", "dawda", Procedures.Count, 15);
foreach (Procedure procedure in procedures) {
Procedures.Add(procedure);
}
} catch(Exception ex) {
Debug.WriteLine(ex);
} finally {
IsBusy = false;
}
}
public void ClearProcedures() {
Procedures.Clear();
}
}
AvgScore
属性定义在哪里?我猜它在ProceduresViewModel
中。你应该展示那个 ViewModel 的相关部分。 - JulianDraw()
方法中设置了断点以查看它被调用的频率和Score
的值吗? - Julian