<tfoot id='A1cSm'></tfoot>

      <i id='A1cSm'><tr id='A1cSm'><dt id='A1cSm'><q id='A1cSm'><span id='A1cSm'><b id='A1cSm'><form id='A1cSm'><ins id='A1cSm'></ins><ul id='A1cSm'></ul><sub id='A1cSm'></sub></form><legend id='A1cSm'></legend><bdo id='A1cSm'><pre id='A1cSm'><center id='A1cSm'></center></pre></bdo></b><th id='A1cSm'></th></span></q></dt></tr></i><div id='A1cSm'><tfoot id='A1cSm'></tfoot><dl id='A1cSm'><fieldset id='A1cSm'></fieldset></dl></div>
        <bdo id='A1cSm'></bdo><ul id='A1cSm'></ul>

      <legend id='A1cSm'><style id='A1cSm'><dir id='A1cSm'><q id='A1cSm'></q></dir></style></legend>
    1. <small id='A1cSm'></small><noframes id='A1cSm'>

    2. Uwp导航示例和重点控制

      时间:2023-09-13
      • <bdo id='xnqnr'></bdo><ul id='xnqnr'></ul>
        <tfoot id='xnqnr'></tfoot>
            <tbody id='xnqnr'></tbody>

            <small id='xnqnr'></small><noframes id='xnqnr'>

                <legend id='xnqnr'><style id='xnqnr'><dir id='xnqnr'><q id='xnqnr'></q></dir></style></legend>

                <i id='xnqnr'><tr id='xnqnr'><dt id='xnqnr'><q id='xnqnr'><span id='xnqnr'><b id='xnqnr'><form id='xnqnr'><ins id='xnqnr'></ins><ul id='xnqnr'></ul><sub id='xnqnr'></sub></form><legend id='xnqnr'></legend><bdo id='xnqnr'><pre id='xnqnr'><center id='xnqnr'></center></pre></bdo></b><th id='xnqnr'></th></span></q></dt></tr></i><div id='xnqnr'><tfoot id='xnqnr'></tfoot><dl id='xnqnr'><fieldset id='xnqnr'></fieldset></dl></div>

                本文介绍了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导航示例和重点控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:垃圾收集何时在 C# 中触发? 下一篇:在 Windows 10 上获取已连接的蓝牙设备列表

                相关文章

                1. <legend id='NrWHM'><style id='NrWHM'><dir id='NrWHM'><q id='NrWHM'></q></dir></style></legend>
                  <i id='NrWHM'><tr id='NrWHM'><dt id='NrWHM'><q id='NrWHM'><span id='NrWHM'><b id='NrWHM'><form id='NrWHM'><ins id='NrWHM'></ins><ul id='NrWHM'></ul><sub id='NrWHM'></sub></form><legend id='NrWHM'></legend><bdo id='NrWHM'><pre id='NrWHM'><center id='NrWHM'></center></pre></bdo></b><th id='NrWHM'></th></span></q></dt></tr></i><div id='NrWHM'><tfoot id='NrWHM'></tfoot><dl id='NrWHM'><fieldset id='NrWHM'></fieldset></dl></div>
                  <tfoot id='NrWHM'></tfoot>

                  <small id='NrWHM'></small><noframes id='NrWHM'>

                    • <bdo id='NrWHM'></bdo><ul id='NrWHM'></ul>