Bind in XAML to one item in a List, but that item is obtained by another property on the same ViewModel

huangapple go评论86阅读模式
英文:

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&lt;CarDataViewModel&gt; _carDataViewModels;

    public MainViewModel()
    {
        CarDataViewModels = new();
        for (int i = 0; i &lt; 22; i++)
        {
            CarDataViewModel carDataViewModel = new();
            CarDataViewModels.Add(carDataViewModel);
        }
    }
}

MauiProgram.cs

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp&lt;App&gt;()
                        .UseMauiCommunityToolkit()
                        .ConfigureFonts(fonts =&gt;
			{
				fonts.AddFont(&quot;OpenSans-Regular.ttf&quot;, &quot;OpenSansRegular&quot;);
				fonts.AddFont(&quot;OpenSans-Semibold.ttf&quot;, &quot;OpenSansSemibold&quot;);
			});

                builder.Services.AddSingleton&lt;MainViewModel&gt;();
                return builder.Build();
	}
}

AppShell.xaml.cs

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();

        BindingContext = ServiceHelper.GetService&lt;MainViewModel&gt;();
        ...
    }

    ...
}

When I access directly using an explicit list index it works...

...

&lt;!-- This updates fine --&gt;
&lt;Label Text={Binding CarDataViewModels[0].CurrentLapTimeInMS} /&gt;

...

...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];

huangapple
  • 本文由 发表于 2023年7月3日 07:10:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定