【问题标题】:UWP get text from AutoSuggestBox after Find icon tapped轻按“查找”图标后,UWP 从 AutoSuggestBox 获取文本
【发布时间】:2017-08-31 18:15:45
【问题描述】:

我想在点击“查找”图标后从 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文件。

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

【问题讨论】:

  • 点击图标时不会调用autoSuggestBox_QuerySubmitted 吗?
  • 不,只有在我选择了建议之后
  • 不,它应该被调用。在if (args.ChosenSuggestion != null) 处设置断点。
  • 它有效,我添加了“String selectedText = autoSuggestBox.Text;”在这个 if 和 else 语句中。谢谢:)
  • 是的,看看我的回答,它应该做你想做的事。您可以改用args.QueryText。 :)

标签: c# uwp win-universal-app uwp-xaml


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-01-20
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2015-11-09
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多