英文:
Bind in XAML to one item in a List, but that item is obtained by another property on the same ViewModel
问题
我遇到了与绑定相关的问题,我不确定哪里出错了。我有一个包含索引属性的 ViewModel,同时在同一个 ViewModel 中还有另一个 ViewModel 的 List<>. 在我的 XAML 中,我试图从列表中的一个 ViewModel 中获取一个属性,这个 ViewModel 由该索引值指示,但除了使用一个字面数字之外,我在 XAML 中尝试的一切都没有成功。在代码后端绑定似乎有点用,但它只更新了一次,然后再也没有更新过,并且它是使用默认模式。
ViewModel 们以不断变化的方式(有时每 1/60 秒一次)通过 UDP 进行单向传输。
CarDataViewModel.cs
public partial class CarDataViewModel : ObservableObject
{
[ObservableProperty]
private uint _currentLapTimeInMS;
...
}
MainViewModel.cs
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private byte _playerIndex;
[ObservableProperty]
private List<CarDataViewModel> _carDataViewModels;
public MainViewModel()
{
CarDataViewModels = new();
for (int i = 0; i < 22; i++)
{
CarDataViewModel carDataViewModel = new();
CarDataViewModels.Add(carDataViewModel);
}
}
}
MauiProgram.cs
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
builder.Services.AddSingleton<MainViewModel>();
return builder.Build();
}
}
AppShell.xaml.cs
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
BindingContext = ServiceHelper.GetService<MainViewModel>();
...
}
...
}
当我直接使用显式列表索引时,它可以正常工作...
...
<!-- This updates fine -->
<Label Text={Binding CarDataViewModels[0].CurrentLapTimeInMS} />
...
...但该数组将根据运行的汽车数量而增长,因此 UDP 流也会为我提供我应该关注的数组索引,我也将其作为 MainViewModel 中的属性。
英文:
I'm having this issue with binding and I'm not sure what I'm doing wrong. I have a ViewModel which contains an index property and in the same ViewModel theres's also a List<> of another ViewModel. In my XAML I'm trying to get a property from one of the ViewModels in the List indicated by that index value, but everything I tried in XAML except using a literal number hasn't worked. It kinda did worked binding on codebehind but it only updated once and never again, and it was using the default mode.
The ViewModels are being constantly (every 1/60th of a second in some cases) one way via UDP.
CarDataViewModel.cs
public partial class CarDataViewModel : ObservableObject
{
[ObservableProperty]
private uint _currentLapTimeInMS;
...
}
MainViewModel.cs
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private byte _playerIndex;
[ObservableProperty]
private List<CarDataViewModel> _carDataViewModels;
public MainViewModel()
{
CarDataViewModels = new();
for (int i = 0; i < 22; i++)
{
CarDataViewModel carDataViewModel = new();
CarDataViewModels.Add(carDataViewModel);
}
}
}
MauiProgram.cs
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
builder.Services.AddSingleton<MainViewModel>();
return builder.Build();
}
}
AppShell.xaml.cs
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
BindingContext = ServiceHelper.GetService<MainViewModel>();
...
}
...
}
When I access directly using an explicit list index it works...
...
<!-- This updates fine -->
<Label Text={Binding CarDataViewModels[0].CurrentLapTimeInMS} />
...
...but that array will grow depending on the amount of cars running, so the UDP stream also provides me with the array index I should be keeping and eye on, which I also have it as a property in my MainViewModel.
答案1
得分: 0
需要创建另一个属性
[ObservableProperty]
private CarDataViewModel _selectedCarData;
然后在更新 _playerIndex
时更新它
SelectedCarData = CarDataViewModels[PlayerIndex];
英文:
you need to create another property
[ObservableProperty]
private CarDataViewModel _selectedCarData;
and then update it whenever you update _playerIndex
SelectedCarData = CarDataViewModels[PlayerIndex];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论