Uwp导航的例子,并着重于控制 [英] Uwp navigation example and focusing on control

查看:182
本文介绍了Uwp导航的例子,并着重于控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < 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.Focus之后,Focus函数在其他地方被调用。

在AppShell.xaml.cs中,您可以找到以下代码: > private void OnNavigatedToPage(object sender,NavigationEventArgs e)
{
//成功导航后,将键盘焦点置于加载页面
if(e.Content为Page&& 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();

$ / code>

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

您的代码在页面本身订阅页面加载的事件。并且您的代码将在AppShell中的Page_Loaded函数之前执行。所以,如果你只是简单地注释((Page)sender).Focus(FocusState.Programmatic) ; Page_Loaded函数中的。你会得到你想要的。我不确定这一行的确切目的是什么。但一切似乎都很好。



如果在注释掉该行后发现有问题,我们也可以解决这个问题。

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

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


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


$ / code>

希望这可以帮到你。


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 does not receive the focus. I try this code in a "normal" windows universal app - it is work. What do you advise?

解决方案

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

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.

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.

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.

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;
    }
}

Hope this can help you.

这篇关于Uwp导航的例子,并着重于控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆