C#-UWP AutoSuggestBox-向上/向下箭头键触发SuggestionChosen

打开建议列表后,向上/向下箭头键会自动触发“ SuggestionChosen”事件.我想知道是否有办法拦截此按键?我曾尝试使用KeyDown和KeyUp事件来捕获这些按键,但是,SuggestionChosen事件发生在KeyDown / Up事件之前.这有效地迫使用户选择列表上的第一个或最后一个建议.鼠标单击或触摸选择就可以了.

我只想在输入AutoSuggestBox时忽略方向键.或者,不要强迫用户使用箭头键选择第一个或最后一个项目.有什么办法可以做到这一点?谢谢

XAML

<AutoSuggestBox Name="EmailSuggestBox" 
                PlaceholderText="Email"
                Text="{Binding Customer.EmailAddress, Mode=TwoWay}"
                TextChanged="EmailSuggestBox_TextChanged"
                QuerySubmitted="EmailSuggestBox_QuerySubmitted"
                SuggestionChosen="EmailSuggestBox_SuggestionChosen"
                LostFocus="EmailSuggestBox_LostFocus"
                KeyUp="EmailSuggestBox_KeyUp"
                KeyDown="EmailSuggestBox_KeyDown" />

方法(注意:vm.EmailOptions只是电子邮件域建议的列表)

    private void EmailSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
    {
        try
        {
            if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
            {
                if (sender.Text.Contains('@'))
                {
                    var vm = this.DataContext as ProspectInformationEntryViewModel;
                    var d = sender.Text.Split('@');
                    var domain = d.LastOrDefault();
                    List<String> _emailSuggestion = vm.EmailOptions.Where(x => x.StartsWith(domain)).ToList();
                    sender.ItemsSource = _emailSuggestion;
                }
            }
        }
        catch (Exception)
        { }

    }
    private void EmailSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
    {
        try
        {
            if (args.ChosenSuggestion != null)
            {
                sender.ItemsSource = null;
            }
        }
        catch (Exception)
        { }
    }

    private void EmailSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
    {
        try
        {
            var domain = args.SelectedItem.ToString();
            var temp = sender.Text.Split('@');
            var identifier = temp.FirstOrDefault();
            sender.Text = identifier + "@" + domain;
            sender.ItemsSource = null;
        }
        catch (Exception)
        { }
    }
    private void EmailSuggestBox_KeyUp(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Down || e.Key == Windows.System.VirtualKey.Up)
        {
            e.Handled = true;
        }
    }

    private void EmailSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Down || e.Key == Windows.System.VirtualKey.Up)
        {
            e.Handled = true;
        }
    }

解决方法:

I would just like to ignore the arrow keys while typing in the AutoSuggestBox.

根据您的要求,您可以使用ProcessKeyboardAccelerators事件来拦截按键或按键按下.

private void Autosbox_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{

    if (args.Key == VirtualKey.Down || args.Key == VirtualKey.Up)
    {
        args.Handled = true;
    }
}
上一篇:c#-使用EntityFramework和.NET Native时出现PlatformNotSupportedException


下一篇:[UWP]使用PointLight并实现动画效果