【问题标题】:Unable to see Bindings on XAML page in Xamarin.forms无法在 Xamarin.forms 中的 XAML 页面上看到绑定
【发布时间】: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


【解决方案1】:

关闭IsGroupingEnabledItemsSource中的数据没有分组,所以你不应该告诉ListView它是。

要使用分组,请查看 ListView 文档中的 Grouping 部分

【讨论】:

  • 感谢我忽略了在我的 XAML 中包含该属性。
猜你喜欢
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多