在我的Xamarin Forms应用程序中,当用户重点关注Entry
元素时,我想对其进行验证。仅当用户在键盘上点击“输入”时,才使用Completed
事件。我尝试使用Unfocused
,但此事件在用户在Entry元素上键入时触发,我真的不明白为什么。
仅当Entry元素不集中时,我才能执行一段代码吗?
我有10个ViewCell
元素,例如:
<ViewCell >
<StackLayout Orientation="Horizontal" Padding="13, 5" >
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" Spacing="1">
<Label Text="Matricola" VerticalOptions="Center" HorizontalOptions="StartAndExpand" ></Label>
<Entry x:Name="matricola" Text="{Binding Paziente.MatricolaPaziente, Mode=TwoWay}" HorizontalOptions="FillAndExpand" Completed="Entry_Completed" Unfocused="Entry_Completed" ></Entry>
<Label Text="{Binding RegistrationValidator.MatricolaError, Mode=OneWay}" HorizontalOptions="FillAndExpand" TextColor="IndianRed"></Label>
</StackLayout>
</StackLayout>
</ViewCell>
后面的代码:
private async void Entry_Completed(object sender, EventArgs e){
// Some code here
}
此外,事件会触发意外的随机发件人(其他Entry
元素,始终带有e.IsFocused == false
)
答案 0 :(得分:1)
我尝试过这样的示例表格
<ContentPage.Content>
<StackLayout Spacing="20" Padding="15">
<Label Text="Text" FontSize="Medium" />
<Entry Text="{Binding Item.Text}" d:Text="Item name" FontSize="Small" Unfocused="Entry_Unfocused" Completed="Entry_Completed" />
<Label Text="Description" FontSize="Medium" />
<Editor Text="{Binding Item.Description}" d:Text="Item description" FontSize="Small" Margin="0" />
</StackLayout>
</ContentPage.Content>
在后面的代码中:
private void Entry_Unfocused(object sender, FocusEventArgs e)
{
var txt = ((Entry)sender).Text;
DisplayAlert("Info", $"Value of text after entry lost focus : {txt} ", "OK");
}
private void Entry_Completed(object sender, EventArgs e)
{
var txt = ((Entry)sender).Text;
DisplayAlert("Info", $"Value when Enter Key is tapped : {txt} ", "OK");
}
一切正常!在再次查看代码时,似乎您已进入列表视图。也许问题可能出在这里。
答案 1 :(得分:1)
Entry
内部的ListView
焦点问题似乎发生在Android(8.1和9.0)(Xamarin.Forms 4.5.0.356)上(在ListView
的第二行及以后)。
另请参阅“ Entry focus issue inside ListView”。
作为解决方法,请使用CollectionView
(在ViewCell
中不要使用ItemTemplate
)。
没有入门的样本焦点问题:
<CollectionView>
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>First</x:String>
<x:String>Second</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<Entry Text="collectionview" Focused="Entry_Unfocused" Unfocused="Entry_Unfocused" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
在Android(8.1和9.0)上的ListView
中具有入门重点的样本:
<ListView>
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>First</x:String>
<x:String>Second</x:String>
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Entry Text="listview" Focused="Entry_Unfocused" Unfocused="Entry_Unfocused" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
观察焦点事件的代码背后:
void Entry_Unfocused(object sender, FocusEventArgs e)
{
Debug.WriteLine($"Entry_Unfocused:{e.IsFocused}:");
}
void Entry_Focused(object sender, FocusEventArgs e)
{
Debug.WriteLine($"Entry_Focused:{e.IsFocused}:");
}
答案 2 :(得分:0)
我认为无论焦点或焦点是否集中都会触发未聚焦事件,但是事件上的FocusEventArgs
应该有一个名为IsFocused
的布尔值,该布尔值应显示条目是否仍然聚焦。您是否尝试过检查?看起来您未聚焦的事件处理程序在可能使用更具体的FocusEventArgs
答案 3 :(得分:0)
每当条目失去焦点时都会引发未聚焦事件。我编写了一个简单的代码来进行测试和验证。
Xaml:
<ListView ItemsSource="{Binding list}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Entry Text="{Binding Name}" Unfocused="Entry_Unfocused" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
代码:
public partial class EntryPage : ContentPage
{
public ObservableCollection<Person> list { get; set; }
public EntryPage()
{
InitializeComponent();
list = new ObservableCollection<Person>()
{
new Person (){ Name="A"},
new Person (){ Name="B"}
};
this.BindingContext = this;
}
private void Entry_Unfocused(object sender, FocusEventArgs e)
{
}
}
public class Person
{
public string Name { get; set; }
}