【问题标题】:Xamarin Forms Tags in CarouselViewCarouselView 中的 Xamarin 表单标签
【发布时间】:2016-07-06 06:22:05
【问题描述】:

我在我的应用程序中使用新的CarouselView 控件来显示文本/文章的集合。集合中的每篇文章还包含一个文本字符串数组,我将其称为标签。我想使用允许数据绑定的控件在轮播视图中水平显示这些标签。预期的外观应该是这样的,就在正文下方: Expected look of tags

关于如何实现这一点的任何想法?

【问题讨论】:

  • 每个视图都可以有一个名为“Tittle”或“Tag”的全局变量,您可以在所有代码中访问它:public string Tag1 = "Tag 1";

标签: xamarin.ios xamarin.android xamarin.forms


【解决方案1】:

嗯,首先是好/坏消息:ListView 将允许您呈现一组可绑定的标签,但不幸的是,它目前不支持 Horizontal 方向,因此无法满足您的需求。

下一个最好的方法是水平的StackLayout 包含在ScrollView 中:

<ScrollView Orientation="Horizontal">
  <StackLayout x:Key="tags" Orientation="Horizontal">
  </StackLayout>
</ScrollView>

并在绑定上下文或该属性更改时使用来自 ViewModel 的数据填充 StackLayout。下面的示例使用 Label 视图填充 StackLayout,这些视图使用名为“TagStyleName”的样式设置样式。

// Note: If this code doesn't work, someone else wrote it. 
protected override OnBindingContextChanged() {
  var vm = this.BindingContext as MyViewModelClass;
  if (vm == null) return;
  vm.PropertyChanged += (o, e) => {
    if (e.PropertyName == "Tags") {
      WatchTagsForChanges();
      RefreshTags();
    }
  };
}

private void WatchTagsForChanges() {
  var vm = this.BindingContext as MyViewModelClass;
  if (vm == null) return;

  vm.Tags.CollectionChanged += (o, e) => RefreshTags();
}

private void RefreshTags() {
  var vm = this.BindingContext as MyViewModelClass;
  if (vm == null) return;

  this.tags.Children.Clear()
  foreach (var tag in vm.Tags) {
    this.tags.Children.Add(new Label{ Text = tag, Style = "TagStyleName" }));
  }
}

【讨论】:

  • 谢谢。您的解决方案以前曾想过,但我将其搁置为“PLAN B”,因为我正在寻找一种可以在轮播视图中绑定的解决方案,以避免在轮播被刷卡时延迟加载儿童.我遇到了这个 [链接] (github.com/daniel-luberda/DLToolkit.Forms.Controls/blob/master/…)。我没有取得太大的成功,并且由于项目的时间限制,我现在使用了“B计划”。因此,您的回答就是答案。
猜你喜欢
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多