UWP 在点击查找图标后从 AutoSuggestBox 获取文本

UWP get text from AutoSuggestBox after Find icon tapped

我想在点击“查找”图标后从 AutoSuggestBox 获取文本。有什么解决办法吗?

<StackPanel
                Grid.Row="0"
                Grid.Column="0">

                <AutoSuggestBox
                    x:Name="autoSuggestBox"
                    Height="40"
                    Margin="24,44,24,0"
                    Text=""
                    FontSize="32"
                    PlaceholderText="Wyszukaj serial..."
                    QuerySubmitted="autoSuggestBox_QuerySubmitted"
                    SuggestionChosen="autoSuggestBox_SuggestionChosen"
                    TextChanged="autoSuggestBox_TextChanged"
                    QueryIcon="Find"/>
            </StackPanel>

这是 XML 文件。

private void autoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
    {
        if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
        {
            var auto = (AutoSuggestBox)sender;
            var suggestion = suggestions.Where(p => p.StartsWith(auto.Text, StringComparison.OrdinalIgnoreCase)).ToArray();
            auto.ItemsSource = suggestion;
        }
    }

    private void autoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
    {
        if (args.ChosenSuggestion != null)
        {
            autoSuggestBox.Text = args.ChosenSuggestion.ToString();
        }
    }

    private void autoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
    {
        var selectedItem = args.SelectedItem.ToString();
        sender.Text = selectedItem;
    }

这是cs文件。

点击查找图标后,我想获取输入文本并在其他功能中使用该字符串。

应该解雇 QuerySubmitted。因此,在这种情况下,您正在寻找 else if

private void autoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
    if (args.ChosenSuggestion != null && args.ChosenSuggestion is YourModelItem yourModelItem)
    {
        // When an item is selected...
    }
    else if (!string.IsNullOrEmpty(args.QueryText))
    {
        // When the search box is filled with something...
    }
}