ProtectedLocalStorage.SetAsync() 不保存嵌套属性?

ProtectedLocalStorage.SetAsync() doesn't save nested properties?

我正在 Blazor(服务器端)开发一个高尔夫记分卡网络应用程序。我希望它执行以下操作之一:

我的代码:

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        Console.WriteLine("Loading scorecard from local storage...");
        var result = await ProtectedLocalStorage.GetAsync<Scorecard>("scorecard");
        if (result.Success)
            scorecard = result.Value;

        StateHasChanged();
    }
    else
    {
        Console.WriteLine("Saving scorecard to local storage...");
        if (scorecard != null)
            await ProtectedLocalStorage.SetAsync("scorecard", scorecard);
    }
}

我已检查 GetSet 是否按预期达到。我还检查了 Set 上记分卡的值是否正确。但是一旦我再次达到 Get,分数就会重置为 0。

ScoresScorecard class 上 HoleScore 的列表,如下所示:

public class Scorecard
{
    (...)
    public List<HoleScore> Scores { get; } = new List<HoleScore>();
}

public record HoleScore
{
    public Guid PlayerId { get; set; }
    public int HoleNumber { get; set; }
    public int NumberOfStrokes { get; set; }
}

它是值 NumberOfStrokes,每次加载时都会重置为零(或者可能永远不会保存)。这是为什么?

(de)seializer 需要能够设置属性。

//public List<HoleScore> Scores { get; } = new List<HoleScore>();
  public List<HoleScore> Scores { get; set; } = new List<HoleScore>();

或者,如评论中所述:

  public List<HoleScore> Scores { get; init; } = new List<HoleScore>();

提供意外访问。