UWP 专注于 AutoSuggestBox

rav*_*mar 3 c# xaml uwp

我的AutoSuggestBox可见性设置为Collapsed。在同一视图页面中,我有一个按钮。单击按钮时,我想显示AutoSuggestBox. 当查询已提交或AutoSuggestBox失去焦点时,我想再次隐藏它。

这是 AutoSuggestBox 和按钮:

<AutoSuggestBox Name="MainAutoSuggestBox"   
                Grid.Row="2"
                GotFocus="MainAutoSuggestBox_GotFocus"
                Visibility="Collapsed"
                QueryIcon="Find"                               
                QuerySubmitted="MainAutoSuggestBox_QuerySubmitted"
                LostFocus="MainAutoSuggestBox_LostFocus"/>

<Button Name="TopBarSearchButton"
        Content="Button"
        Click="TopBarSearchButton_Click"/>
Run Code Online (Sandbox Code Playgroud)

我的代码隐藏:

class SomePage : page
{
    ...
    ...

    private void MainAutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
    {
        //only for testing purposes
    }

    private void MainAutoSuggestBox_LostFocus(object sender, RoutedEventArgs e)
    {
        MainAutoSuggestBox.Visibility = Visibility.Collapsed;

        //put focus on the page
        this.Focus(FocusState.Programmatic);
    }

    private void MainAutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
    {
        //Do something
        //Works fine
    }

    private void TopBarSearchButton_Click(object sender, RoutedEventArgs e)
    {
        HandleSearchButtonClick();
    }

    private void HandleSearchButtonClick()
    {
        if (MainAutoSuggestBox.Visibility == Visibility.Collapsed)
        {
            MainAutoSuggestBox.Visibility = Visibility.Visible;
            MainAutoSuggestBox.Focus(FocusState.Programmatic);
        }
        else
        {       
            MainAutoSuggestBox.Visibility = Visibility.Collapsed;
        }    
    }
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是,当我TopBarSearchButton 第一次单击该按钮时,MainAutoSuggestBox切换的可见性会发生变化,但焦点并未设置在MainAutoSuggestBox. 但从第二次开始,它按预期工作,即单击按钮后,MainAutoSuggestBox切换的可见性以及焦点将设置在 上MainAutoSuggestBox

在调试过程中,我发现当第一次单击按钮时,控制流到达了MainAutoSuggestBox.Focus(FocusState.Programmatic);里面的行HandleSearchButtonClick(),但它从未到达

private void MainAutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
Run Code Online (Sandbox Code Playgroud)

方法,但从第二次开始,它确实击中了

private void MainAutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
Run Code Online (Sandbox Code Playgroud)

方法。

Jus*_* XL 5

我们首先要弄清楚为什么第一次没有设置焦点。

First, subscribe to the Loaded event of the MainAutoSuggestBox, inside the handler, you will find that the RenderSize of MainAutoSuggestBox is <0,0>. This makes sense, as you have set the Visibility of the control to Collapsed in XAML, resulting in the control to ignore all size changing events.

So, by the time the second line of below code hit for the first time, although the Visibility is set to Visible, the control has yet to be fully rendered, so the following Focus() will take no effect. After that, the control is done rendering, and that's why from the second time on the Focus() will now work properly.

    MainAutoSuggestBox.Visibility = Visibility.Visible;
    MainAutoSuggestBox.Focus(FocusState.Programmatic);
Run Code Online (Sandbox Code Playgroud)

There's a few ways to fix this. First is to subscribe to the SizeChanged event and then in the handler, only when the old size is <0,0> and new size is something else, you know it's completed rendering, call the Focus() there.

Or more easily, don't set it to Collapsed in XAML but do it in the Loaded event handler as it will be fully rendered before collapsing -

MainAutoSuggestBox.Loaded += (s, e) => MainAutoSuggestBox.Visibility = Visibility.Collapsed;
Run Code Online (Sandbox Code Playgroud)