我要做的是:在this article中所示的android(原生)小部件Text
上为AutoCompleteTextView
属性分配双向绑定。
它失败的地方尽管如此,完全相同的方法适用于Switch
,但EditText
和AutoCompleteTextView
都失败了。我尝试在绑定表达式中使用TextChanged
,AfterTextChanged
和FocusChange
作为UpdateSourceEventName
的事件。每个都失败了。在TextChanged
的情况下,值会反向更新,因为光标位置会一直恢复到起始点,而在FocusChange
中,事件似乎根本不会被触发。
重新创建问题的示例代码:
XAML
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:NativeViewTest"
xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
xmlns:formsAndroid="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
x:Name="_parent"
x:Class="NativeViewTest.NativeViewTestPage">
<StackLayout Margin="10">
<Label TextColor="Green" Text="Switch + CheckedChange: Binding works as expected." />
<androidWidget:Switch x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Is Switch On?"
Checked="{Binding Path=IsSwitchOn, Source={x:Reference _parent}, Mode=TwoWay, UpdateSourceEventName=CheckedChange}" />
<Label Text="{Binding Path=IsSwitchOn, StringFormat='IsSwitchOn: {0}', Source={x:Reference _parent}" />
<Label TextColor="Navy" Text="AutoCompleteTextView + TextChanged: Value is updated in reverse." />
<androidWidget:AutoCompleteTextView x:Arguments="{x:Static formsAndroid:Forms.Context}"
Text="{Binding Path=TextChangedText, Source={x:Reference _parent}, Mode=TwoWay, UpdateSourceEventName=TextChanged}" />
<Label Text="{Binding Path=TextChangedText, StringFormat='TextChangedText: {0}', Source={x:Reference _parent}" />
<Label TextColor="Red" Text="AutoCompleteTextView + FocusChange: Value is never updated." />
<androidWidget:AutoCompleteTextView x:Arguments="{x:Static formsAndroid:Forms.Context}"
Text="{Binding Path=LostFocusText, Source={x:Reference _parent}, Mode=TwoWay, UpdateSourceEventName=FocusChange}" />
<Label Text="{Binding Path=LostFocusText, StringFormat='LostFocusText: {0}', Source={x:Reference _parent}" />
<Label TextColor="Navy" Text="AutoCompleteTextView + AfterTextChanged: Same issue as text-changed" />
<androidWidget:AutoCompleteTextView x:Arguments="{x:Static formsAndroid:Forms.Context}"
Text="{Binding Path=AfterTextChangedText, Source={x:Reference _parent}, Mode=TwoWay, UpdateSourceEventName=AfterTextChanged}" />
<Label Text="{Binding Path=AfterTextChangedText, StringFormat='AfterTextChangedText: {0}', Source={x:Reference _parent}" />
<Button Text="Update" />
</StackLayout>
</ContentPage>
代码隐藏
public partial class NativeViewTestPage : ContentPage
{
public NativeViewTestPage()
{
InitializeComponent();
}
string _afterTextChanged;
public string AfterTextChangedText
{
get { return _afterTextChanged; }
set {
_afterTextChanged = value;
OnPropertyChanged(nameof(AfterTextChangedText));
}
}
string _textChangedText = "initial value";
public string TextChangedText
{
get { return _textChangedText; }
set
{
_textChangedText = value;
OnPropertyChanged(nameof(TextChangedText));
}
}
string _lostFocusText;
public string LostFocusText
{
get { return _lostFocusText; }
set
{
_lostFocusText = value;
OnPropertyChanged(nameof(LostFocusText));
}
}
bool _isSwitchOn = false;
public bool IsSwitchOn
{
get { return _isSwitchOn; }
set
{
_isSwitchOn = value;
OnPropertyChanged(nameof(IsSwitchOn));
}
}
}
编辑 - 1
我尝试将绑定模式更改为OneWayToSource
,假设可以解决游标问题 - 但仍会导致完全相同的问题。