如何将数据绑定到对象内的对象属性(嵌套对象的属性)

时间:2018-10-03 15:46:32

标签: c# xamarin.forms

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

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 [ “键”]。每个项目的网址值。现在,我只是尝试显示该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对象的一部分。

我尝试研究了一段时间,但没有结果。有没有办法设置绑定到嵌套对象的属性?这是正确的做法吗? 感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我只在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");
  }
}

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

相关问题