【问题标题】:Put List<T> in ListView将 List<T> 放入 ListView
【发布时间】:2021-04-13 10:54:49
【问题描述】:

我有一个模型:

public class BandModel
{
    public string BandName { get; set; }
    public CountryModel Country { get; set; }
    public GenreModel Genre{ get; set; }
    public DateTime DateOfBirth { get; set; }
}

还有一个类会返回这些类的列表

public class FullBandProcessor        
{
    public static async Task<List<BandModel>> LoadFullBandInformation()
    {
        string url = "http://localhost:11727/api/Bands";

        using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
        {
            if (response.IsSuccessStatusCode)
            {
                List<BandModel> result = await response.Content.ReadAsAsync<List<BandModel>>();

                return result;
            }
            else
            {
                throw new Exception(response.ReasonPhrase);
            }
        }
    }
}

我需要在 WPF 中显示这些对象的列表,但我不知道该怎么做 - 它在 WPF 中是完全绿色的。

在这里,我尝试在其中使用ObservableCollection、implement get,以便它调用LoadFullBandInformation,以便稍后它可以绑定到xaml 中的这个属性,但有些东西对我不起作用。

private ObservableCollection<BandModel> _BandModels; 


public ObservableCollection<BandModel> bandModels
    {
        get
        {
            return _BandModels = FullBandProcessor.LoadFullBandInformation();
        }
        private set
        {
            
        }
    }

【问题讨论】:

  • 当您调用 LoadFullBandInformation() 时可能缺少 await
  • 不,等待没有帮助
  • 您在此处编写的代码甚至都无法编译,所以我们怎么能调试您看到的不同问题?

标签: c# .net wpf


【解决方案1】:

您需要使用数据绑定和项目模板来实现这一点。另外,您实际上需要在某处等待您致电FullBandProcessor.LoadFullBandInformation();。这是更改为工作的 C# 代码:

public class YourClass
{
    public ObservableCollection<BandModel> BandModels { get; private set; } = new ObservableCollection<BandModel>() // initialise it as empty

    // Constructor
    public YourClass()
    {
        SetBandModels();
    }

    private async Task SetBandModels()
    {
        // Add a try/ catch block
        var bands = await FullBandProcessor.LoadFullBandInformation().ConfigureAwait(false);
        BandModels = new ObservableCollection<BandModel>(bands);
    }
}

然后在您的 XAML 中,您创建一个 &lt;ListView&gt; 和它的项目模板并绑定。像这样:

<ListView ItemsSource="{Binding BandModels}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Label Content={"Binding BandName}" /> // Add more controls with more bindings
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【讨论】:

  • BandModels = new ObservableCollection(bands); BandModels 显示错误:“无法将属性或索引器‘YourClass.BandModels’分配给——它是只读的”。
  • 哎呀,对不起,更新了代码并在属性描述中添加了private set;
【解决方案2】:

LoadFullBandInformation 返回List&lt;BandModel&gt;,而_BandModelsObservableCollection

你应该替换:

 get
 {
    return _BandModels = FullBandProcessor.LoadFullBandInformation();
 }

 get
 {
    return _BandModels = new ObservableCollection<BandModel> (FullBandProcessor.LoadFullBandInformation());
 }

【讨论】:

  • LoadFullBandInformation 必须是 awaited,这不会编译。
猜你喜欢
  • 1970-01-01
  • 2019-05-20
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 2012-02-24
相关资源
最近更新 更多