【问题标题】:Xamarin.UWP Custom ViewCell has problems with the bindingsXamarin.UWP 自定义 ViewCell 存在绑定问题
【发布时间】:2017-11-21 00:24:38
【问题描述】:

我创建了一个应该在我的 ListView 中使用的自定义 ViewCell 相同的代码适用于 Android,但不适用于 UWP(Windows Phone 和 Windows 桌面类似)。

该项目是 Xamarin Forms。

当我在我的 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...
        }
    }

绑定正在整个 ViewModel 中完成 POCO 如下所示

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

虚拟机 公共类 ProductDetailsPageViewModel : BaseViewModel { #region 字段 私有只读 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 上附加了一个 Tapped 事件...我可以看到绑定...

编辑 1

我已经注释掉了 Grid 代码,因为我发现其他一些开发人员在绑定 Grid 元素中的元素时遇到了问题... 仍然无法正常工作,奇怪的是它正确“解析”了 XAML 元素,但无法进行绑定....我做错了什么?

这就是它在 Android 上的样子

【问题讨论】:

  • 这不是一个不同的问题,也许文本出于某种原因是白色的?您能否将TextColor 设置为红色或其他任何内容以排除它:)
  • 我已经更新了主线程...仍然无法正常工作,如果我将 Text={Binding} 替换为一些硬编码值,它将呈现。

标签: xaml listview xamarin xamarin.forms xamarin.uwp


【解决方案1】:

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

 this.BindingContext = this;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-16
    • 2018-12-20
    • 1970-01-01
    • 2016-08-24
    • 2016-08-25
    • 2017-06-18
    • 2021-04-05
    • 2017-02-07
    相关资源
    最近更新 更多