我正在尝试创建一个MVVM应用程序,该应用程序从API中提取数据并将数据放入用于导航程序的列表中。
我遇到的问题是列表产生了此错误,我无法弄清原因:
绑定:绑定到“ Avalonia.Controls.TextBlock”。“文本”时出错: “在“属性”上找不到CLR属性“名称””
对于上下文,'attributes'类包含'name'变量,我已经确认了name变量是预先填充的(程序在我试图形成列表之前就打印了name变量)。
XAML代码(MainWindow.xaml):
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:RT_Stream_App.ViewModels;assembly=RT_Stream_App"
Icon="resm:RT_Stream_App.Assets.avalonia-logo.ico"
Title="RT Stream App">
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<ListBox Items="{Binding CompanyList}" HorizontalAlignment="Left" Width="512" Height="512" Margin="20,20,0,10" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel DataContext="attributes">
<TextBlock Text="{Binding name}" TextAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- <TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/> -->
</Window>
MainWindowViewModel.cs
namespace RT_Stream_App.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
// use => instead of = for assigning
// public string Greeting => "I am testing!";
public companies.APIData siteList => loadCompanies();
//public string Greeting => TestLoop(siteList);
public ObservableCollection<companies.companyData> CompanyList => siteList.data;
public companies.APIData loadCompanies()
{
// This takes the API data for companies and converts it into a useable class
companies.APIData toReturn = JsonConvert.DeserializeObject<companies.APIData>(new WebClient().DownloadString("https://svod-be.roosterteeth.com/api/v1/channels"));
return toReturn;
}
}
}
类别数据(companies.cs):
namespace RT_Stream_App.Classes
{
public class companies
{
/// <summary>
/// Root of the JSON
/// </summary>
public class APIData
{
public ObservableCollection<companyData> data = new ObservableCollection<companyData>();
}
/// <summary>
/// A class that holds the data for each company (Name and link mostly)
/// </summary>
public class companyData
{
public attributeData attributes = new attributeData();
public linkData links = new linkData();
}
/// <summary>
/// Contains the company name
/// </summary>
public class attributeData
{
public string name { get; set; }
}
/// <summary>
/// Contains link data for the next step
/// </summary>
public class linkData
{
public string shows { get; set; }
}
}
}
我在做什么错,需要更改什么?
更新:我尝试将XAML中的DataTemplate更改为以下内容:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding attributes.name}" TextAlignment="Center" />
</StackPanel>
</DataTemplate>
哪个会产生此错误:
绑定:绑定到“ Avalonia.Controls.TextBlock”。“文本”时出错: “找不到CLR属性的“属性”
<DataTemplate>
<StackPanel DataContext="{Binding attributes}">
<TextBlock Text="{Binding name}" TextAlignment="Center" />
</StackPanel>
</DataTemplate>
产生此错误:
绑定:绑定到错误 “ Avalonia.Controls.StackPanel”。“ DataContext”:“找不到CLR 属性“属性”
修复更新:从Kekekeks的答案中,我弄清楚了为什么我的程序无法正常工作,现在列出了负载。由于使用JSON.NET,我担心使用构造函数,但是我对所有类进行了以下更改,并且程序显示了预期的列表
类数据(companies.cs)已更新: 公共类APIData {
public APIData()
{
this.data = new ObservableCollection<companyData>();
}
public ObservableCollection<companyData> data {
get;
set;
}
}
答案 0 :(得分:1)
您正在将字符串“属性”设置为DataContext
。然后Binding
在System.String
上找不到属性“ name”。
删除DataContext="attributes"
,然后将绑定替换为Text="{Binding attributes.name}"