gpt4 book ai didi

c# - 获取json后数据绑定(bind)到listview

转载 作者:行者123 更新时间:2023-12-03 10:32:00 25 4
gpt4 key购买 nike

我正在尝试了解 MVVM,并且很难让我的 ListView 显示可观察集合中的项目。我将 json 放入我设置的类中没有问题,但是让 xaml 读取绑定(bind)没有成功。我不太清楚如何设置它以便能够从 data2 读取属性。有人可以向我指出我做错了什么吗?

来自 reddit ,我需要获取每个Base.Data.Children.data.title。 0-24个 child ,所以我应该收到24个头衔。

型号

public class Child : BindableBase
{
public string kind { get; set; }
public Data2 data { get; set; }
}

public class Data2 : BindableBase
{
public string title { get; set; }
}

public class Posts : ViewModelBase
{
public string kind { get; set; }
public Data data { get; set; }

internal static Task<Posts> GetFileFromApplicationUriAsync(Uri uri)
{
throw new NotImplementedException();
}
}

查看
         <ScrollViewer x:Name="PostsScrollViewer" HorizontalScrollBarVisibility="Hidden" 
Height="Auto" Width="Auto">
<ListView x:Name="PostListBox" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Children}" SelectedItem="{Binding Children}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Children.Data.title}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</ScrollViewer>

查看型号

View 模型库
 public abstract class ViewModelBase : INotifyPropertyChanged
{
#region Property Changed Event Handler
public void SetPropertyChanged(string propertyName)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion Property Changed Event Handler
}

后 View 模型
    public class PostsViewModel : ViewModelBase
{
private ObservableCollection<Child> children = new ObservableCollection<Child>();
private ObservableCollection<Data2> data = new ObservableCollection<Data2>();
private string title;

#region Constructor
public PostsViewModel()
{
GetFrontPagePostsAsync();
}
#endregion Constructor

#region Properties
#region Children
public ObservableCollection<Child> Children
{
get { return this.children; }
set
{
if (value != this.children)
this.children = value;
this.SetPropertyChanged("Children");
}
}
#endregion Children
#endregion Properties
public async void GetFrontPagePostsAsync()
{
using (var client = new HttpClient())
{
try
{
string content = await client.GetStringAsync("http://www.reddit.com/.json");
Posts posts = JsonConvert.DeserializeObject<Posts>(content);
foreach (var i in posts.data.children)
{
children.Add(i);
}
}
catch (Exception e) {
Console.WriteLine(e); }
}
}
}

最佳答案

首先,我假设您的 Children 收藏不是空的

然后,我认为您的问题出在 View 中。它应该是这样的:

查看

<ScrollViewer x:Name="PostsScrollViewer" HorizontalScrollBarVisibility="Hidden" 
Height="Auto" Width="Auto">
<ListView x:Name="PostListBox" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Children}" SelectedItem="{Binding Children}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Data.title}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>

改变的是 TextBlock 的行。
TextBlock (ListView) 的父级绑定(bind)到您的子级列表。这意味着 ListView 的每个 child 都“知道”它需要渲染的对象。在您的情况下,TextBlock 知道指定的子对象属性(取决于项目位置)。
如果您将页面绑定(bind)到某种 查看型号 ,您不需要将每个 child 绑定(bind)到
<TextBlock Text="{Binding ViewMode.Title}" />

但您只需将其绑定(bind)到:
<TextBlock Text="{Binding Title}" />

关于c# - 获取json后数据绑定(bind)到listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55206055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com