ContentView 可以接收焦点吗?

Can a ContentView receive focus?

我有一个元素,我希望在使用方向键导航到该元素并响应“输入”按钮时突出显示该元素。我假设我可以用一个聚焦元素来做到这一点,并且正在尝试设置我的 ContentView 来处理这种交互。

ContentView class 继承自 VisualElement 并且有一个 Focus() 方法。文档指出要使 Focus() 方法起作用,“[元素] 必须能够接收焦点。”

如何使元素“能够获得焦点”?对于这个基本示例,它似乎无法在 UWP 或 Tizen 中工作。 Tab 键、单击、箭头键、以编程方式设置焦点...没有任何设置 IsFocused 为 true,我的回调从未被调用。

using System;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace MyProject.Components
{
    public class FocusMe : ContentView
    {
        public FocusMe ()
        {
            Content = new StackLayout {
                Children = {
                    new Label { Text = "Welcome to Xamarin.Forms!" }
                }
            };
            Focused += FocusMe_Focused;
            Task.Run(async () => {
                await Task.Delay(5000);
                Focus(); // false
                _ = IsFocused; // false
            });
        }

        private void FocusMe_Focused(object sender, FocusEventArgs e)
        {
            // never called
        }
    }
}

Can a ContentView receive focus?

根据测试,ContentView不会收到Focused事件。但是,Frame布局可以接收focused事件,frame继承自ContentView,可以用Frame代替。请注意,您需要在其中放置可聚焦控件,例如按钮。

如果你在 Tizen TV 上工作,检查一下如何 TV.UIControls,它是电视应用程序的扩展库

如果您想在视图上对按键事件做出反应,我建议在 TV.UIControls 上使用 ContentButton 视图 它基于 ContentView 但像按钮一样工作

这是指南 https://samsung.github.io/Tizen.TV.UIControls/guides/ContentButton.html