我有两个条目。当我点击入口1上的任何东西时,我希望得到"是"。当我在第2项上输入任何东西时,我希望得到" no& quot;
问题:
当我点击条目1时,条目2改变并获得值"Noo"但是条目1也改变了,得到了值&;yes &;
问题:
在不改变1号记录的情况下,轻按1号记录如何改变2号记录条目2
也是一样以下是Xaml代码:<Entry ClassId="1" x:Name="myWord1"TextChanged="OnEntryTextChange"/>
<Entry ClassId="2" x:Name="myWord2" TextChanged="OnEntryTextChange"/>
代码:
private async void OnEntryTextChange(object sender, TextChangedEventArgs e)
{
var EntryTapped = (Xamarin.Forms.Entry)sender;
Device.BeginInvokeOnMainThread(() => {
if (EntryTapped.ClassId == "1")
{
myWord2.Text="Noo";
}
else if (EntryTapped.ClassId == "2")
{
myWord1.Text="yess";
}
});
}
谢谢你的帮助
您可以使用Focused
事件代替TextChanged
事件。
<StackLayout>
<Entry ClassId="1" x:Name="myWord1" Focused="EntryFocused"/>
<Entry ClassId="2" x:Name="myWord2" Focused="EntryFocused"/>
</StackLayout>
private void EntryFocused(object sender, FocusEventArgs e)
{
var EntryTapped = (Xamarin.Forms.Entry)sender;
if (EntryTapped.ClassId == "1")
{
myWord2.Text = "Noo";
}
else if (EntryTapped.ClassId == "2")
{
myWord1.Text = "yess";
}
}
有几种方法:
使用绑定在这种情况下,您将有2个私有变量和2个公共变量,并将条目绑定到每个变量。查看此链接如何实现INotifyPropertyChanged
private string entry1String;
private string entry2String;
public string Entry1String {
get => entry1String;
set
{
entry2String = "Noo";
entry1String = value;
OnPropertyChanged(Entry1String);
OnPropertyChanged(Entry2String);
}
}
public string Entry2String {
get => entry2String;
set
{
entry1String = "Yees";
entry2String = value;
OnPropertyChanged(Entry1String);
OnPropertyChanged(Entry2String);
}
}
另一种方法是使用变量作为信号量。当变量为True
时,该方法不能同时被其他方法触发。
private bool semaphoreFlag=false;
private async void OnEntryTextChange(object sender, TextChangedEventArgs e)
{
if(semaphoreFlag) return;
semaphoreFlag=true;
var EntryTapped = (Xamarin.Forms.Entry)sender;
Device.BeginInvokeOnMainThread(() => {
if (EntryTapped.ClassId == "1")
{
myWord2.Text="Noo";
}
else if (EntryTapped.ClassId == "2")
{
myWord1.Text="yess";
}
});
semaphoreFlag=false;
}