我试图将Frame中的这个Entry提取到Xamarin中的自定义元素中,以获得一个可重用的Entry,其边框仅在顶部和底部:
<Frame xmlns="..."
HasShadow="False"
CornerRadius="0"
Padding="0, 1, 0, 1"
BackgroundColor="#c0c0c0">
<Entry Padding="20, 10, 20, 10"
Placeholder="{Binding Placeholder}"
Text="{Binding Text}"
BackgroundColor="#ffffff" />
</Frame>
代码隐藏:
public partial class CbSingleEntry : Frame
{
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(CbSingleEntry));
public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create("Placeholder", typeof(string), typeof(CbSingleEntry));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public string Placeholder
{
get { return (string)GetValue(PlaceholderProperty); }
set { SetValue(PlaceholderProperty, value); }
}
public CbSingleEntry()
{
InitializeComponent();
BindingContext = this;
}
}
当我尝试使用此自定义字段时,Placeholder和Text属性设置正确,但我无法将它们绑定到类中的属性:
// this one works fine
<local:CbSingleEntry Placeholder="Company" Text="My Company" />
// Placeholder works, but Text is always empty
<local:CbSingleEntry Placeholder="Company" Text="{Binding Company}" />
我可以确认Company有一个值,因为使用普通文本字段它可以正常工作:
// This one works as expected, Text is displayed from binded attribute
<Entry Placeholder="Company" Text="{Binding Company}" />
2条答案
按热度按时间rekjcdws1#
原因:在本例中,您在
CbSingleEntry
中设置了BindingContext因此ContentPage中的绑定将不再起作用。
解决方案:
您可以在CbSingleEntry中修改代码
在xaml中
在代码后面
3pmvbmvn2#
简单的事情是设置条目文本BindingModeTwoWay
例如:public static readonly BindableProperty CitytxtProperty = BindableProperty.Create(nameof(Citytxt),typeof(string),typeof(CustomAddressView),“",BindingMode.TwoWay);