Why is my ListView not populating with data? No errors and I can see the data in the console in VS, .NET Maui application using JSON from API

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

Why is my ListView not populating with data? No errors and I can see the data in the console in VS, .NET Maui application using JSON from API

问题

以下是您的Model类 - Models文件夹中的Posts.cs部分:

  1. using Newtonsoft.Json;
  2. using System.Text.Json.Serialization;
  3. namespace App.Models
  4. {
  5. public class CommunityBannerImage
  6. {
  7. [JsonPropertyName("mimetype")] public string Mimetype { get; set; }
  8. [JsonPropertyName("width")] public int Width { get; set; }
  9. [JsonPropertyName("height")] public int Height { get; set; }
  10. [JsonPropertyName("size")] public int Size { get; set; }
  11. [JsonPropertyName("averageColor")] public string AverageColor { get; set; }
  12. [JsonPropertyName("url")] public string Url { get; set; }
  13. [JsonPropertyName("copies")] public List<Copy> Copies { get; set; }
  14. }
  15. public class CommunityProPic
  16. {
  17. [JsonPropertyName("mimetype")] public string Mimetype { get; set; }
  18. [JsonPropertyName("width")] public int Width { get; set; }
  19. [JsonPropertyName("height")] public int Height { get; set; }
  20. [JsonPropertyName("size")] public int Size { get; set; }
  21. [JsonPropertyName("averageColor")] public string AverageColor { get; set; }
  22. [JsonPropertyName("url")] public string Url { get; set; }
  23. [JsonPropertyName("copies")] public List<Copy> Copies { get; set; }
  24. }
  25. public class Copy
  26. {
  27. [JsonPropertyName("copyId")] public string CopyId { get; set; }
  28. [JsonPropertyName("width")] public int Width { get; set; }
  29. [JsonPropertyName("height")] public int Height { get; set; }
  30. [JsonPropertyName("size")] public int Size { get; set; }
  31. [JsonPropertyName("url")] public string Url { get; set; }
  32. [JsonPropertyName("objectFit")] public string ObjectFit { get; set; }
  33. }
  34. public class Image
  35. {
  36. [JsonPropertyName("mimetype")] public string Mimetype { get; set; }
  37. [JsonPropertyName("width")] public int Width { get; set; }
  38. [JsonPropertyName("height")] public int Height { get; set; }
  39. [JsonPropertyName("size")] public int Size { get; set; }
  40. [JsonPropertyName("averageColor")] public string AverageColor { get; set; }
  41. [JsonPropertyName("url")] public string Url { get; set; }
  42. [JsonPropertyName("copies")] public List<Copy> Copies { get; set; }
  43. }
  44. public class Link
  45. {
  46. [JsonPropertyName("url")] public string Url { get; set; }
  47. [JsonPropertyName("hostname")] public string Hostname { get; set; }
  48. [JsonPropertyName("image")] public Image Image { get; set; }
  49. }
  50. public class Post
  51. {
  52. [JsonPropertyName("id")] public string Id { get; set; }
  53. [JsonPropertyName("type")] public string Type { get; set; }
  54. [JsonPropertyName("publicId")] public string PublicId { get; set; }
  55. [JsonPropertyName("userId")] public string UserId { get; set; }
  56. [JsonPropertyName("username")] public string Username { get; set; }
  57. [JsonPropertyName("userGroup")] public string UserGroup { get; set; }
  58. [JsonPropertyName("userDeleted")] public bool UserDeleted { get; set; }
  59. [JsonPropertyName("isPinned")] public bool IsPinned { get; set; }
  60. [JsonPropertyName("communityId")] public string CommunityId { get; set; }
  61. [JsonPropertyName("communityName")] public string CommunityName { get; set; }
  62. [JsonPropertyName("communityProPic")] public CommunityProPic CommunityProPic { get; set; }
  63. [JsonPropertyName("communityBannerImage")] public CommunityBannerImage CommunityBannerImage { get; set; }
  64. [JsonPropertyName("title")] public string Title { get; set; }
  65. [JsonPropertyName("body")] public object Body { get; set; }
  66. [JsonPropertyName("link")] public Link Link { get; set; }
  67. [JsonPropertyName("locked")] public bool Locked { get; set; }
  68. [JsonPropertyName("lockedBy")] public object LockedBy { get; set; }
  69. [JsonPropertyName("lockedAt")] public object LockedAt { get; set; }
  70. [JsonPropertyName("upvotes")] public int Upvotes { get; set; }
  71. [JsonPropertyName("downvotes")] public int Downvotes { get; set; }
  72. [JsonPropertyName("hotness")] public object Hotness { get; set; }
  73. [JsonPropertyName("createdAt")] public DateTime CreatedAt { get; set; }
  74. [JsonPropertyName("editedAt")] public object EditedAt { get; set; }
  75. [JsonPropertyName("lastActivityAt")] public DateTime LastActivityAt { get; set; }
  76. [JsonPropertyName("deleted")] public bool Deleted { get; set; }
  77. [JsonPropertyName("deletedAt")] public object DeletedAt { get; set; }
  78. [JsonPropertyName("deletedContent")] public bool DeletedContent { get; set; }
  79. [JsonPropertyName("noComments")] public int NoComments { get; set; }
  80. [JsonPropertyName("comments")] public object Comments { get; set; }
  81. [JsonPropertyName("commentsNext")] public object CommentsNext { get; set; }
  82. [JsonPropertyName("userVoted")] public bool UserVoted { get; set; }
  83. [JsonPropertyName("userVotedUp")] public bool? UserVotedUp { get; set; }
  84. }
  85. [JsonObject]
  86. public class Root
  87. {
  88. [JsonPropertyName("posts")] public List<Post> Posts { get; set; }
  89. [JsonPropertyName("next")] public string Next { get; set; }
  90. }
  91. }

这是您的ViewModel类 - ViewModels文件夹中的PostsViewModel.cs部分:

  1. using App.Models;
  2. using System.Text.Json;
  3. using System.Windows.Input;
  4. namespace App.ViewModels
  5. {
  6. public class PostsViewModel : BaseViewModel
  7. {
  8. private string _type;
  9. public string Type { get => _type; set { _type = value; OnPropertyChanged(nameof(Type)); } }
  10. private string _username;
  11. public string Username { get => _username; set { _username = value; OnPropertyChanged(nameof(Username)); } }
  12. private string _communityName;
  13. public string CommunityName { get => _communityName; set { _communityName = value; OnPropertyChanged(nameof(CommunityName)); } }
  14. private string _title;
  15. public string Title { get => _title; set { _title = value; OnPropertyChanged(nameof(Title)); } }
  16. private string _body;
  17. public string Body { get => _body; set { _body = value; OnPropertyChanged(nameof(Body)); } }
  18. public ICommand LoadPostsCommand { get; set; }
  19. public PostsViewModel()
  20. {
  21. LoadPostsCommand = new Command(async () => await LoadPosts());
  22. }
  23. private async Task LoadPosts()
  24. {
  25. var url = "https://discuit.net/api/posts";
  26. var client = new HttpClient();
  27. var response = await client.GetAsync(url);
  28. var successCode = await client.GetAsync(url);
  29. // validate connection
  30. if (successCode.IsSuccessStatusCode)
  31. {
  32. Console.WriteLine("-----------------------------------
  33. <details>
  34. <summary>英文:</summary>
  35. Here is my Model class - Posts.cs in Models folder:

using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace App.Models
{
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class CommunityBannerImage
{
[JsonPropertyName("mimetype")] public string Mimetype { get; set; }
[JsonPropertyName("width")] public int Width { get; set; }
[JsonPropertyName("height")] public int Height { get; set; }
[JsonPropertyName("size")] public int Size { get; set; }
[JsonPropertyName("averageColor")] public string AverageColor { get; set; }
[JsonPropertyName("url")] public string Url { get; set; }
[JsonPropertyName("copies")] public List<Copy> Copies { get; set; }
}

  1. public class CommunityProPic
  2. {
  3. [JsonPropertyName(&quot;mimetype&quot;)] public string Mimetype { get; set; }
  4. [JsonPropertyName(&quot;width&quot;)] public int Width { get; set; }
  5. [JsonPropertyName(&quot;height&quot;)] public int Height { get; set; }
  6. [JsonPropertyName(&quot;size&quot;)] public int Size { get; set; }
  7. [JsonPropertyName(&quot;averageColor&quot;)] public string AverageColor { get; set; }
  8. [JsonPropertyName(&quot;averageColor&quot;)] public string Url { get; set; }
  9. [JsonPropertyName(&quot;copies&quot;)] public List&lt;Copy&gt; Copies { get; set; }
  10. }
  11. public class Copy
  12. {
  13. [JsonPropertyName(&quot;copyId&quot;)] public string CopyId { get; set; }
  14. [JsonPropertyName(&quot;width&quot;)] public int Width { get; set; }
  15. [JsonPropertyName(&quot;height&quot;)] public int Height { get; set; }
  16. [JsonPropertyName(&quot;size&quot;)] public int Size { get; set; }
  17. [JsonPropertyName(&quot;url&quot;)] public string Url { get; set; }
  18. [JsonPropertyName(&quot;objectFit&quot;)] public string ObjectFit { get; set; }
  19. }
  20. public class Image
  21. {
  22. [JsonPropertyName(&quot;mimetype&quot;)] public string Mimetype { get; set; }
  23. [JsonPropertyName(&quot;width&quot;)] public int Width { get; set; }
  24. [JsonPropertyName(&quot;height&quot;)] public int Height { get; set; }
  25. [JsonPropertyName(&quot;size&quot;)] public int Size { get; set; }
  26. [JsonPropertyName(&quot;averageColor&quot;)] public string AverageColor { get; set; }
  27. [JsonPropertyName(&quot;url&quot;)] public string Url { get; set; }
  28. [JsonPropertyName(&quot;copies&quot;)] public List&lt;Copy&gt; Copies { get; set; }
  29. }
  30. public class Link
  31. {
  32. [JsonPropertyName(&quot;url&quot;)] public string Url { get; set; }
  33. [JsonPropertyName(&quot;hostname&quot;)] public string Hostname { get; set; }
  34. [JsonPropertyName(&quot;image&quot;)] public Image Image { get; set; }
  35. }
  36. public class Post
  37. {
  38. [JsonPropertyName(&quot;id&quot;)] public string Id { get; set; }
  39. [JsonPropertyName(&quot;type&quot;)] public string Type { get; set; }
  40. [JsonPropertyName(&quot;publicId&quot;)] public string PublicId { get; set; }
  41. [JsonPropertyName(&quot;userId&quot;)] public string UserId { get; set; }
  42. [JsonPropertyName(&quot;username&quot;)] public string Username { get; set; }
  43. [JsonPropertyName(&quot;userGroup&quot;)] public string UserGroup { get; set; }
  44. [JsonPropertyName(&quot;userDeleted&quot;)] public bool UserDeleted { get; set; }
  45. [JsonPropertyName(&quot;isPinned&quot;)] public bool IsPinned { get; set; }
  46. [JsonPropertyName(&quot;communityId&quot;)] public string CommunityId { get; set; }
  47. [JsonPropertyName(&quot;communityName&quot;)] public string CommunityName { get; set; }
  48. [JsonPropertyName(&quot;communityProPic&quot;)] public CommunityProPic CommunityProPic { get; set; }
  49. [JsonPropertyName(&quot;communityBannerImage&quot;)] public CommunityBannerImage CommunityBannerImage { get; set; }
  50. [JsonPropertyName(&quot;title&quot;)] public string Title { get; set; }
  51. [JsonPropertyName(&quot;body&quot;)] public object Body { get; set; }
  52. [JsonPropertyName(&quot;link&quot;)] public Link Link { get; set; }
  53. [JsonPropertyName(&quot;locked&quot;)] public bool Locked { get; set; }
  54. [JsonPropertyName(&quot;lockedBy&quot;)] public object LockedBy { get; set; }
  55. [JsonPropertyName(&quot;lockedAt&quot;)] public object LockedAt { get; set; }
  56. [JsonPropertyName(&quot;upvotes&quot;)] public int Upvotes { get; set; }
  57. [JsonPropertyName(&quot;downvotes&quot;)] public int Downvotes { get; set; }
  58. [JsonPropertyName(&quot;hotness&quot;)] public object Hotness { get; set; }
  59. [JsonPropertyName(&quot;createdAt&quot;)] public DateTime CreatedAt { get; set; }
  60. [JsonPropertyName(&quot;editedAt&quot;)] public object EditedAt { get; set; }
  61. [JsonPropertyName(&quot;lastActivityAt&quot;)] public DateTime LastActivityAt { get; set; }
  62. [JsonPropertyName(&quot;deleted&quot;)] public bool Deleted { get; set; }
  63. [JsonPropertyName(&quot;deletedAt&quot;)] public object DeletedAt { get; set; }
  64. [JsonPropertyName(&quot;deletedContent&quot;)] public bool DeletedContent { get; set; }
  65. [JsonPropertyName(&quot;noComments&quot;)] public int NoComments { get; set; }
  66. [JsonPropertyName(&quot;comments&quot;)] public object Comments { get; set; }
  67. [JsonPropertyName(&quot;commentsNext&quot;)] public object CommentsNext { get; set; }
  68. [JsonPropertyName(&quot;userVoted&quot;)] public bool UserVoted { get; set; }
  69. [JsonPropertyName(&quot;userVotedUp&quot;)] public bool? UserVotedUp { get; set; }
  70. }
  71. [JsonObject]
  72. public class Root
  73. {
  74. [JsonPropertyName(&quot;posts&quot;)] public List&lt;Post&gt; Posts { get; set; }
  75. [JsonPropertyName(&quot;next&quot;)] public string Next { get; set; }
  76. }

}

  1. Here is my ViewModel - PostsViewModel.cs in ViewModels folder:

using App.Models;
using System.Text.Json;
using System.Windows.Input;

namespace App.ViewModels;

public class PostsViewModel : BaseViewModel
{
private string _type;
public string Type { get => _type; set { _type = value; OnPropertyChanged(nameof(Type)); } }
private string _username;
public string Username { get => _username; set { _username = value; OnPropertyChanged(nameof(Username)); } }
private string _communityName;
public string CommunityName { get => _communityName; set { _communityName = value; OnPropertyChanged(nameof(CommunityName)); } }
private string _title;
public string Title { get => _title; set { _title = value; OnPropertyChanged(nameof(Title)); } }
private string _body;
public string Body { get => _body; set { _body = value; OnPropertyChanged(nameof(Body)); } }

  1. public ICommand LoadPostsCommand { get; set; }
  2. public PostsViewModel()
  3. {
  4. LoadPostsCommand = new Command(async () =&gt; await LoadPosts());
  5. }
  6. private async Task LoadPosts()
  7. {
  8. var url = $&quot;https://discuit.net/api/posts&quot;;
  9. var client = new HttpClient();
  10. var response = await client.GetAsync(url);
  11. var successCode = await client.GetAsync(url);
  12. // validate connection
  13. if (successCode.IsSuccessStatusCode)
  14. {
  15. Console.WriteLine(&quot;-----------------------------------&quot;);
  16. Console.WriteLine(&quot;Code 200 - Successful Connection to REST API&quot;);
  17. Console.WriteLine(&quot;-----------------------------------&quot;);
  18. var data = await response.Content.ReadAsStringAsync();
  19. var options = new JsonSerializerOptions
  20. {
  21. PropertyNameCaseInsensitive = true,
  22. };
  23. var posts = JsonSerializer.Deserialize&lt;Posts&gt;(data, options);
  24. Console.WriteLine(&quot;-----------------------------------&quot;);
  25. Console.WriteLine(&quot;Data: &quot; + data);
  26. Console.WriteLine(&quot;Posts: &quot; + posts);
  27. Console.WriteLine(&quot;-----------------------------------&quot;);
  28. var Type = posts.Type;
  29. var Username = posts.Username;
  30. var CommunityName = posts.CommunityName;
  31. var Title = posts.Title;
  32. var Body = posts.Body;
  33. }
  34. else
  35. {
  36. Console.WriteLine(&quot;Error connecting to API&quot;);
  37. }
  38. }

}

  1. Here is my Content Page code behind - HomePage.xaml.cs in Views folder:

using App.ViewModels;

namespace App.Views;
public partial class HomePage : ContentPage
{
public HomePage()
{
InitializeComponent();
BindingContext = new PostsViewModel();
}
}

  1. Here is my Content Page - HomePage.xaml in Views folder

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App.Views.HomePage" Title="Posts"
xmlns:local="clr-namespace:App"
xmlns:vm="clr-namespace:App.ViewModels"
x:DataType="vm:PostsViewModel"
>

  1. &lt;ListView ItemsSource=&quot;{Binding LoadPostsCommand}&quot;&gt;
  2. &lt;ListView.Header&gt;
  3. &lt;VerticalStackLayout
  4. Spacing=&quot;25&quot;
  5. Padding=&quot;30,0&quot;
  6. VerticalOptions=&quot;Center&quot;&gt;
  7. &lt;Button
  8. x:Name=&quot;UpdateBtn&quot;
  9. Text=&quot;Load Posts&quot;
  10. Command=&quot;{Binding LoadPostsCommand}&quot;
  11. Margin=&quot;10&quot;
  12. /&gt;
  13. &lt;/VerticalStackLayout&gt;
  14. &lt;/ListView.Header&gt;
  15. &lt;ListView.ItemTemplate&gt;
  16. &lt;DataTemplate&gt;
  17. &lt;TextCell Text=&quot;{Binding Title}&quot; Detail=&quot;{Binding Body}&quot; /&gt;
  18. &lt;/DataTemplate&gt;
  19. &lt;/ListView.ItemTemplate&gt;
  20. &lt;/ListView&gt;

</ContentPage>

  1. and here is the BaseViewModel.cs page that HomePage inherits:

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Disc.ViewModels;

public abstract class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

  1. protected void OnPropertyChanged([CallerMemberName] string propertyName = &quot;&quot;)
  2. {
  3. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  4. }
  5. protected bool SetProperty&lt;TValue&gt;(ref TValue backingField, TValue value, [CallerMemberName] string propertyName = &quot;&quot;)
  6. {
  7. if (Comparer&lt;TValue&gt;.Default.Compare(backingField, value) == 0)
  8. {
  9. return false;
  10. }
  11. backingField = value;
  12. OnPropertyChanged(propertyName);
  13. return true;
  14. }

}

  1. and finally, here is my debug output:

[DOTNET] -----------------------------------
[DOTNET] Code 200 - Successful Connection to REST API
[DOTNET] -----------------------------------
[monodroid-assembly] open_from_bundles: failed to load assembly Newtonsoft.Json.dll
Loaded assembly: /data/data/com.companyname.app/files/.override/Newtonsoft.Json.dll [External]
[DOTNET] -----------------------------------
[DOTNET] Data: {"posts":[{"id":"1775d55eb7515d9d1e2d7481","type":"text","publicId":"pTJMsSk9","userId":"176caa4ff743f7b0a2fa3ec2","username":"pH_low","userGroup":"mods","userDeleted":false,"isPinned":false,"communityId":"1770a5ebb222dcb14b21f6cd","communityName":"DiscoApp","communityProPic":null,"communityBannerImage":null,"title":"Update - July 17th","body":"Hello everyone!
[DOTNET] Posts: App.Models.Posts
[DOTNET] -----------------------------------

  1. Thanks so much for taking a look!!
  2. [1]: https://stackoverflow.com/questions/76683374/how-to-get-json-response-into-a-listview-in-net-maui
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. 创建一个类型为`Root`的属性在你的`VM`中。
  7. ```csharp
  8. [ObservableProperty]
  9. Root 数据 { get; set; }

将你的数据反序列化到该属性中。

  1. Data = JsonSerializer.Deserialize<Root>(data, options);

然后将Data.Posts用作你的ItemsSource

  1. <ListView ItemsSource="{Binding Data.Posts}">
英文:

create a property in your VM of type Root

  1. [ObservableProperty]
  2. Root data { get; set; }

deserialize your data into that property

  1. Data = JsonSerializer.Deserialize&lt;Root&gt;(data, options);

then use Data.Posts as your ItemsSource

  1. &lt;ListView ItemsSource=&quot;{Binding Data.Posts}&quot;&gt;

huangapple
  • 本文由 发表于 2023年7月28日 05:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783426.html
匿名

发表评论

匿名网友

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

确定