我必须在 xamarin 的 CarouselPage 中显示超过 10 个 ContentPage,并且在每个 ContentPage 中有许多标签、按钮、条目。那么我如何才能当前聚焦 VisualElement。
我找到了事件 VisualElement.Focused , VisualElement.Unfocused , VisualElement.FocusChangeRequested .
但没有人能帮助我。
最佳答案
虽然 VisualElement 有 Focused 和 UnFocused 事件,但并不是所有的控件 将响应,因为事件属于父类(super class)。也是因为平台不同。
例如,WebView 的Focused 和UnFocused 事件将在 iOS 上有效,但在 Android 上无效。
在您的情况下,Label 和 Button 在大多数情况下不会调用事件。 Entry 将在编辑模式(输入)时获得焦点,而在结束编辑时失去焦点。
因此,如果您确实想要获取当前获得焦点的元素,您可以在 ContentPage 中定义一个属性。
public object FocusedElement;
private void FocusEvent(object sender, FocusEventArgs e)
{
FocusedElement = sender; //sender here is the element , like label or entry
}
private void UnfocusEvent(object sender, FocusEventArgs e)
{
FocusedElement = null;
}
在 xaml 中
定义每个元素的Focused和UnFocused事件
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
Focused="FocusEvent" Unfocused="Label_Unfocused"
VerticalOptions="CenterAndExpand" />
<Button Focused="FocusEvent" Unfocused="UnfocusEvent" Text="111"/>
<Entry Focused="FocusEvent" Unfocused="UnfocusEvent" HeightRequest="50" WidthRequest="100" />
关于c# - 如何在 xamarin 中获得焦点 VisualElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63133815/