【问题标题】:Custom ViewCell Bindable Properties does not work自定义 ViewCell 可绑定属性不起作用
【发布时间】:2018-12-20 12:50:38
【问题描述】:

我为 TableView 创建了 ViewCell(自定义 EntryCell),但该值没有绑定到我的 ViewModel。 如果我创建默认的 EntryCell,那么一切正常,但我的自定义 ViewCell 没有绑定。

我就是不明白怎么回事,真的希望得到你的帮助

EntryCell.xaml:

<?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="AproReportsClient.UI.Controls.EntryCell"
          x:Name="This"
          BindingContext="{x:Reference This}">

    <Entry Text="{Binding Text}"
           Placeholder="{Binding Placeholder}"         
           TextColor="{Binding TextColor}"
           FontSize="{Binding FontSize}" Margin="12, 8, 8, 8"
           HorizontalOptions="Fill" VerticalOptions="FillAndExpand"/>

</ViewCell>

EntryCell.xaml.cs:

namespace AproReportsClient.UI.Controls
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class EntryCell : ViewCell
    {
        public static readonly BindableProperty PlaceholderProperty =
            BindableProperty.Create(propertyName: nameof(Placeholder),
                                    returnType:   typeof(string),
                                    declaringType:typeof(EntryCell),
                                    defaultValue: string.Empty);

        public string Placeholder
        {
            get => (string)GetValue(PlaceholderProperty);
            set => SetValue(PlaceholderProperty, value);
        }


        public static readonly BindableProperty TextProperty =
            BindableProperty.Create(propertyName: nameof(Text),
                                    returnType:   typeof(string),
                                    declaringType:typeof(EntryCell),
                                    defaultValue: string.Empty);

        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }


        public static readonly BindableProperty TextColorProperty =
            BindableProperty.Create(propertyName: nameof(TextColor),
                                    returnType:   typeof(Color),
                                    declaringType:typeof(EntryCell),
                                    defaultValue: Color.Black);

        public Color TextColor
        {
            get => (Color)GetValue(TextColorProperty);
            set => SetValue(TextColorProperty, value);
        }


        public static readonly BindableProperty FontSizeProperty =
            BindableProperty.Create(propertyName: nameof(FontSize),
                                    returnType:   typeof(double),
                                    declaringType:typeof(EntryCell),
                                    defaultValue: 10d);

        public double FontSize
        {
            get => (double)GetValue(FontSizeProperty);
            set => SetValue(FontSizeProperty, value);
        }


        public EntryCell ()
        {
            InitializeComponent ();
        }
    }
}

页面:

//....

<TableView Grid.Row="1" 
                       HasUnevenRows="True">
                <TableRoot>
                    <TableSection>
                        <EntryCell Text="{Binding Title}"/>

                        <controls:EntryCell Placeholder="{extensions:Translate Title}"
                                            Text="{Binding Title}"
                                            TextColor="{StaticResource PrimaryColor}"
                                            FontSize="{StaticResource HeaderFontSize}"/>
                    </TableSection>

                    <TableSection Title="{extensions:Translate AdditionalFields}">
                        <controls:EmptyCell EmptyHeight="150"/>
                    </TableSection>
                </TableRoot>
            </TableView>

//....

我还在 TextChanged 事件处理程序中检查了 text 属性为 null 。没有设置,但是为什么呢?

【问题讨论】:

    标签: uitableview xaml xamarin.forms


    【解决方案1】:

    尝试在 Cell 构造函数中设置 BindingContext。

     public EntryCell ()
     {
          InitializeComponent ();
          BindingContext = this;
     }
    

    【讨论】:

    • 谢谢,但没有帮助。 Get 方法总是在属性中执行,但 Set 不是。
    【解决方案2】:

    看来,ViewCell 中的属性绑定有问题。有人可以创建一个扩展 XF EntryCell 的新 CustomEntryCell。 OnAppearing 应该在此类中被覆盖。在 OnAppearing 方法中,EntryCell 的属性可以使用 BindedContext 归档。 BindedContext 是包含 Title 的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多