首页 > 解决方案 > 如何将数据绑定到对象内对象的属性(嵌套对象的属性)

问题描述

我在将数据绑定到我在 Xamarin 中创建的自定义视图时遇到问题。我是 Xamarin 的新手,但有几年的编程经验。我正在从数字访问管理 API 中提取数据,并将其反序列化为对象“项目”。

public partial class Item
{
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("external_id")]
    public string ExternalId { get; set; }

    [JsonProperty("filename")]
    public string Filename { get; set; }

    [JsonProperty("last_update_date")]
    public DateTimeOffset LastUpdateDate { get; set; }

    [JsonProperty("thumbnails")]
    public Dictionary<string, Thumbnail> Thumbnails { get; set; }

    [JsonProperty("_links")]
    public Links Links { get; set; }

}

为了简单起见,我省略了一些我没有使用的不必要的字段。

在该 Item 类的属性中,有一个字典<string, Thumbnail> Thumbnails

“缩略图”类如下所示:

public partial class Thumbnail
{
    [JsonProperty("url")]
    public Uri Url { get; set; }

    [JsonProperty("valid_until")]
    public DateTimeOffset ValidUntil { get; set; }
}

现在这是我的问题:在我设置的自定义视图中(为了在列表视图中显示有关项目的一些信息,我从 ViewCell 继承),我想将数据绑定到 Thumbnails["Key" ].每个项目的 URL 值。现在,我只是试图显示 url 来测试它的工作,最终我会实际加载照片。

这是我制作的自定义视图:

public class AssetCell : ViewCell
{
    public AssetCell()
    {
        var urlLabel = new Label
        {
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
            FontAttributes = FontAttributes.Bold
        };
        urlLabel.SetBinding(Label.TextProperty, new Binding("Thumbnails[125px].Url"));

        //var titleLabel = new Label
        //{
        //    FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        //    FontAttributes = FontAttributes.Bold
        //};
        //titleLabel.SetBinding(Label.TextProperty, new Binding("Filename"));

        var updatedDateLabel = new Label
        {
            FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
            VerticalTextAlignment = TextAlignment.End,
            HorizontalOptions = LayoutOptions.Start
        };
        updatedDateLabel.SetBinding(Label.TextProperty, new Binding("LastUpdateDate", stringFormat: "{0:MMMM d, yyyy HH:mm}"));

        View = new StackLayout
        {
            Children = { urlLabel, updatedDateLabel },
            Orientation = StackOrientation.Vertical,
            Padding = new Thickness(20, 0, 10, 0)
        };

    }
}

绑定到 Item 类的直接属性(例如注释掉的 titleLabel 和 updatedDateLabel)按预期工作,但我无法访问 Thumbnails 字典的属性(除此之外,Thumbnail 对象的 URI Url 属性)是一部分整个 Item 对象。

我已经尝试研究了一段时间,但没有得出任何结果。有没有办法将绑定设置为嵌套对象的属性?这是正确的方法吗?任何帮助表示赞赏

标签: c#xamarin.forms

解决方案


我只在 XAML 中使用过数据绑定,但对于嵌套对象的数据绑定,大部分情况下应该可以工作。

但是,尝试通过键访问字典可能会导致数据绑定问题。

此外,Xamarin 似乎不喜欢数据绑定时不可隐式转换的类型(例如:您的标签文本属性绑定应该是字符串而不是 Uri 对象)

我建议编写一个转换器:https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters

其中 value 是您的字典,您可以通过转换器参数提供密钥。

然后,您只需使用 value[param] 返回 uri 字符串,假设您正确地从对象中转换了东西。

public class DictionaryToStringConverter : IValueConverter {
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    // cast object to dictionary
    Dictionary<string, Thumbnail> thumbnails = value as Dictionary<string, Thumbnail>;
    // cast parameter to dictionary key 
    String key = parameter as string;

    // retrieve value from dictionary
    Uri uri = thumbnails[key].Url;

    // returning string because label's text property is string
    return uri.AbsoluteUri;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
    throw new NotImplementedException("Not implemented");
  }
}

我很想知道是否有更好的解决方案。


推荐阅读