这直到最近才起作用。我尝试降级 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)
{
}
}