2017-06-18 60 views
0

我创建了应该在我的ListView中使用的自定义ViewCell 相同的代码适用于Android,但不适用于UWP(Windows Phone和Windows桌面)。Xamarin.UWP自定义ViewCell在绑定时遇到问题

该项目是Xamarin形式。

Differences

当我我的ListView中使用它。我只能看到空白单元格。

<?xml version="1.0" encoding="UTF-8"?> 
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="AAA.Extensions.AttributeViewCell" 
      Tapped="AttributeViewCell_OnTapped" 
      > 
<Grid 
    x:Name="AttributeGrid" 
    > 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    <StackLayout 
     Grid.Column="0" 
     > 
     <Label 
      Text="{Binding Code}" 
      TextColor="#000000" 
      /> 
     <Label 
      Text="{Binding Description}" 
      TextColor="#000000" 
      /> 
    </StackLayout> 

    <StackLayout 
     Grid.Column="1" 
     x:Name="DynamicContentStackLayout" 
     > 

    </StackLayout> 
</Grid> 

我身后

[XamlCompilation(XamlCompilationOptions.Compile)] 
    public partial class AttributeViewCell : ViewCell 
    { 
     #region Bindable Properties 
     public static readonly BindableProperty CodeProperty = BindableProperty.Create(nameof(Code),typeof(string),typeof(AttributeViewCell), defaultBindingMode:BindingMode.TwoWay); 
     public static readonly BindableProperty DescriptionProperty = BindableProperty.Create(nameof(Description), typeof(string), typeof(AttributeViewCell), defaultBindingMode:BindingMode.TwoWay); 
     #endregion 

     #region Properties 
     /// <summary> 
     /// Attribute Code 
     /// </summary> 
     public string Code 
     { 
      get => (string) GetValue(CodeProperty); 
      set => SetValue(CodeProperty,value); 
     } 

     /// <summary> 
     /// Attribute's description 
     /// </summary> 
     public string Description 
     { 
      get => (string) GetValue(DescriptionProperty); 
      set => SetValue(DescriptionProperty, value); 
     } 
     #endregion 

     public AttributeViewCell() 
     { 
      InitializeComponent(); 
      this.BindingContext = this; 
     } 

     private void AttributeViewCell_OnTapped(object sender, EventArgs e) 
     { 
      // I CAN SEE BINDINGS here... 
     } 
    } 

代码整个视图模型正在做 的POCO的样子以下

public class Attribute 
    { 
     public string Code { get; set; } 
     public string Description { get; set; } 
    } 

VM 公共CLA结合ss ProductDetailsPageViewModel:BaseViewModel { #region Fields private readonly AnonymousCheckerService _anonymousCheckerService; 私人列表_attributes;

 #endregion 

     #region Properties 

     public List<Attribute> Attributes 
     { 
      get => _attributes; 
      set 
      { 
       _attributes = value; 
       OnPropertyChanged(); 
      } 
     } 

     #endregion 

     //basic ctor 
     public ProductDetailsPageViewModel() 
     { 
      _aaa= new aaa(); 
      Attributes = _aaa.GetInfo("123").Attributes; 
     } 
    } 

最后,ListView控件

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:extensions="clr-namespace:AnonymousCheckerApp.Extensions" 
      x:Class="AnonymousCheckerApp.Views.ProductDetailsPage"> 
    <StackLayout> 
     <ListView 
      ItemsSource="{Binding Attributes}" 
      > 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <extensions:AttributeViewCell 
         Code="{Binding Code}" 
         Description="{Binding Description}" 
         AttributeDetails="{Binding AttributeDetails}" 
         /> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 
</ContentPage> 

and it's codebehind 
public ProductDetailsPage() 
     { 

      var vm = new ProductDetailsPageViewModel(); 
      this.BindingContext = vm; 

      InitializeComponent(); 
     } 

我已经把它贴在viewcell抽头事件......我可以看到绑定...

enter image description here

编辑1

我有通讯因为我发现一些其他开发者在绑定到堆叠在Grid元素中的元素时遇到问题... 仍然无法正常工作,奇怪的是,它正确“解析”了XAML元素,但未能执行绑定....我究竟做错了什么 ?

enter image description here

这是它的外观在Android enter image description here

+0

是不是一个不同的问题,也许文字是白色出于某种原因?你可以设置'TextColor'为红色或任何只是为了排除:) –

+0

我已经更新了主线程...仍然无法正常工作,如果我将Text = {Binding}替换为一些经过编码的值,它将呈现。 – ExtremeSwat

回答

1

我已经测试你的代码并再现您的问题。这条线this.BindingContext = this;是不必要的。由于AttributeViewCell继承ViewCell,因此您可以直接在ListView DataTemplate中使用它。所以请从AttributeViewCell构造函数中删除以下行代码。

this.BindingContext = this; 

enter image description here

相关问题