【发布时间】:2019-01-19 02:22:14
【问题描述】:
我有一个项目没有在 XAML 页面上显示绑定。
我的根对象
public class Notification
{
[JsonProperty("posts")]
public Dictionary<string, List<Post>> posts { get; set; }
}
public class Post
{
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("image_url")]
public Uri imageUrl { get; set; }
[JsonProperty("message")]
public string message { get; set; }
[JsonProperty("time")]
public string time { get; set; }
}
我的服务类(代码隐藏)
var notification = JsonConvert.DeserializeObject<Notification>(apiContent);
List<Post> posts = new List<Post>();
if (notification?.posts != null)
{
foreach (var item in notification.posts.Values)
{
posts.AddRange(item);
}
}
我的 XAML 文件
<Label Text="{Binding title}" />
<Label Text="{Binding message}" />
在 XAML 页面上完成解析后,它不会显示来自 API 的所需标题或消息。当我检查调试器时,它会显示每个帖子的键/值对,因此我知道它从API。
任何帮助将不胜感激,谢谢。
【问题讨论】:
-
您确定您的集合包含数据,并且每个帖子都已被填充?另外,请为您的 ListView 发布完整的 XAML
-
是的,我敢肯定,当我检查调试器项目是否存储在每个帖子的键/值对中时。我更新了我的 XAML 解决方案@Jason
-
看起来不像你为列表视图设置
ItemsSource,你绑定到集合以显示项目 -
NotificationListView.ItemsSource = _schoolNotification;不是将绑定设置为 ListView 吗? -
先尝试使用简单的 TextCell 来验证您的绑定是否正常,然后用 ViewCell 替换
标签: c# xamarin.forms