0

我已经使用Xamarin.Forms定位AndroidUWP平台创建了一个应用程序。(桌面上的 UWP,而不是手机)。
我正在使用主/详细信息导航,当从一个页面导航到另一个页面时,它在 UWP 上保持打开状态。在主页上,只有在 UWP 上,我有一个Entry我想在视图出现时关注的内容,所以我这样做了:

protected override void ViewIsAppearing(object sender, EventArgs e)
{
    base.ViewIsAppearing(sender, e);
    Device.OnPlatform(WinPhone: () =>
    {
        CustomComponent.GainFocus();
    });
}

在我的 CustomComponent 实际上是 StackLayout 里面,我有这个:

public void GainFocus()
{
    CustomEntry.Focus();
}

我的问题是,当我导航到Page2Page3例如返回到MainPage函数被调用时,但该条目从未被聚焦,并且焦点停留在导航面板中导航列表中单击的项目上。我怎样才能解决这个问题,以便我Entry始终获得焦点?

编辑:

我已经进行了一些调试以了解发生了什么。我为我的和
添加了处理程序FocusedUnfocusedCustomEntry

private void CustomEntry_Unfocused(object sender, FocusEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("CustomEntry_Unfocused");
}

private void CustomEntry_Focused(object sender, FocusEventArgs focusEventArgs)
{
    System.Diagnostics.Debug.WriteLine("CustomEntry_Focused");
}

添加后,我运行了我的应用程序并检查了输出。
ViewIsAppearing> 呼叫重点。
在条目外部单击 > 调用 Unfocused
从主页导航到另一个 >不调用 Unfocused,然后返回主页 >不调用 Focused
如果CustomEntry没有聚焦,我导航离开主页并返回 > 呼叫重点。

编辑2:

我尝试以这种方式强制Unfocus事件OnViewIsDisappearing

protected override void ViewIsDisappearing(object sender, EventArgs e)
{
    base.ViewIsDisappearing(sender, e);
    Device.OnPlatform(WinPhone: () =>
    {
        CustomComponent.LoseFocus();
    });
}

在自定义组件内部:

public void LoseFocus()
{
    CustomEntry.Unfocus();
}

在尝试了这个之后,它也没有调用Unfocused......有
什么想法吗?

4

1 回答 1

0

只需添加

CustomEntry.Focus();

InitializeComponent();您想要的页面之后。

于 2017-02-24T13:15:38.850 回答