Xamarin形式的xaml绑定在iOS中失败,但在Android中有效

时间:2018-09-04 13:38:42

标签: xaml xamarin custom-controls

这一直持续到最近。我尝试将XF降级无济于事。它可在Android上运行,但在iOS上失败。

我有一个自定义控件,该控件呈现文本条目,该控件的子类呈现文本的多行版本。 在iOS中,单行的标签在其中显示为空,但在多行中有效。

页面的一部分:

<TableView HasUnevenRows="True" Intent="Form">
                <TableView.Root>
                    <TableSection Title="Details">
                        <view:TextEditor Label="Name" Text="{Binding Name}" />
                        <view:TextAreaEditor Label="Address" Text="{Binding Address}" />

还有我的自定义控件:

public class TextEditor : ViewCell
    {

        public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(TextEditor), null, defaultBindingMode: BindingMode.TwoWay);

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

        public static readonly BindableProperty LabelProperty = BindableProperty.Create("Label", typeof(string), typeof(TextEditor), null, defaultBindingMode: BindingMode.TwoWay);

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        protected TextEntry TextControl { get; set; }
        protected Editor AreaControl { get; set; }
        protected Label LabelControl { get; set; }

        public TextEditor() : this(false)
        {
        }
        public TextEditor(bool isMultiline)
        {
            var gridLayout = new Grid
            {
                Padding = 0,
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                RowDefinitions = {
                    new RowDefinition {
                        Height = new GridLength(1, GridUnitType.Star)
                    }
                },
                ColumnDefinitions = {
                    new ColumnDefinition {
                        Width = new GridLength (1, GridUnitType.Star)
                    },
                    new ColumnDefinition {
                        Width = new GridLength (2, GridUnitType.Star)
                    }
                }
            };

            LabelControl = new Label
            {
                Margin = 10,
                HorizontalTextAlignment = TextAlignment.Start,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.StartAndExpand,
                FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
                LineBreakMode = LineBreakMode.TailTruncation,
                TextColor = Palette.Caption
            };
            LabelControl.SetBinding(Xamarin.Forms.Label.TextProperty, new Binding("Label", BindingMode.TwoWay, source: this));
            gridLayout.Children.Add(LabelControl, 0, 0);


            if (isMultiline)
            {
                Height = 60;
                AreaControl = new Editor
                {
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Margin = 0,
                    Keyboard = Keyboard.Text,
                    VerticalOptions = LayoutOptions.StartAndExpand,
                    //HeightRequest = 60,
                    AutoSize = EditorAutoSizeOption.TextChanges,
                };
                AreaControl.FontSize = Styles.GetEditorFontSize(AreaControl);

                AreaControl.SetBinding(Xamarin.Forms.Editor.TextProperty, new Binding("Text", BindingMode.TwoWay, source: this));
                gridLayout.Children.Add(AreaControl, 1, 0);
            }
            else
            {
                TextControl = new TextEntry
                {
                    //VerticalOptions = LayoutOptions.StartAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    HorizontalTextAlignment = TextAlignment.Start,
                    //HeightRequest = 30,
                    Margin = 0,
                    Keyboard = Keyboard.Text,
                };
                TextControl.FontSize = Styles.GetEditorFontSize(TextControl);

                TextControl.SetBinding(Entry.TextProperty, new Binding("Text", BindingMode.TwoWay, source: this));
                gridLayout.Children.Add(TextControl, 1, 0);
            }

            View = gridLayout;
            View.BindingContext = this;
        }
    }



    public class TextAreaEditor : TextEditor
    {
        public TextAreaEditor() : base(true)
        {
        }
    }

0 个答案:

没有答案