Uwp导航示例和重点控制

Uwp navigation example and focusing on control(Uwp导航示例和重点控制)
本文介绍了Uwp导航示例和重点控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以uwp 导航示例为例用于我的应用程序导航.我需要将焦点设置在 TextBox 上.我在 uwp 导航示例上试用它.对于 BasicPage,我添加了以下代码:

I use uwp navigation example as an example for my application navigation. I need to set the focus on TextBox. I try it on uwp navigation example. For BasicPage I add this code:

   <StackPanel Orientation="Vertical">
        <TextBox x:Name="Test" />
        <TextBox x:Name="Test1" />
    </StackPanel>

    public BasicPage()
    {
        this.InitializeComponent();
        this.Loaded += BasicPage_Loaded;
    }

    private void BasicPage_Loaded(object sender, RoutedEventArgs e)
    {
        Test1.Focus(FocusState.Programmatic);
    }

Test1 没有获得焦点.我在一个普通"的 Windows 通用应用程序中尝试了这个代码——它是有效的.你有什么建议?

Test1 does not receive the focus. I try this code in a "normal" windows universal app - it is work. What do you advise?

推荐答案

这是因为你调用Test1.Focus后,Focus函数在其他地方被调用了.

It is because Focus function gets called in other place after you call the Test1.Focus.

在 AppShell.xaml.cs 中可以找到如下代码:

In AppShell.xaml.cs, you can find the following code:

private void OnNavigatedToPage(object sender, NavigationEventArgs e)
{
    // After a successful navigation set keyboard focus to the loaded page
    if (e.Content is Page && e.Content != null)
    {
        var control = (Page)e.Content;
        control.Loaded += Page_Loaded;
    }
}

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    ((Page)sender).Focus(FocusState.Programmatic);
    ((Page)sender).Loaded -= Page_Loaded;
    this.CheckTogglePaneButtonSizeChanged();
}

上面的代码意味着当你导航到一个页面时,它会订阅页面加载事件并将焦点设置在页面上.

The above code means when you navigate to a page, it will subscribe the page loaded event and set the focus on page.

您的代码在页面本身中订阅页面加载事件.并且您的代码将在 AppShell 中的 Page_Loaded 函数之前执行.所以你没有得到你想要的.

Your code subscribe the page loaded event in the page itself. And your code will be executed before the Page_Loaded function in AppShell. So you didn't get what you want.

因此,如果您只是在 Page_Loaded 函数中注释掉 ((Page)sender).Focus(FocusState.Programmatic);.你会得到你想要的.我不确定那条线的确切目的是什么.但一切似乎都很好.

So if you simply comment out ((Page)sender).Focus(FocusState.Programmatic); in the Page_Loaded function. You will get what you want. I am not sure what's the exact purpose of that line. But everything seems good.

如果您在注释掉该行后发现有问题,我们也可以解决它.加载事件后在 LayoutUpdated 事件中调用一次焦点函数.

If you do find something wrong after comment out that line, we can also work it around. Call the focus function once in LayoutUpdated event after loaded event.

public sealed partial class BasicPage : Page
{
    bool bAfterLoaded = false;
    public BasicPage()
    {
        this.InitializeComponent();
        this.Loaded += BasicPage_Loaded;
        this.LayoutUpdated += BasicPage_LayoutUpdated;
    }

    private void BasicPage_LayoutUpdated(object sender, object e)
    {
        if (bAfterLoaded)
        {
            Test1.Focus(FocusState.Programmatic);
            bAfterLoaded = !bAfterLoaded;
        }
    }

    private void BasicPage_Loaded(object sender, RoutedEventArgs e)
    {
        bAfterLoaded = !bAfterLoaded;
    }
}

希望对你有帮助.

这篇关于Uwp导航示例和重点控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)