How to turn the text prediction on/off on Soft Keyboard while the user is typing

290 Views Asked by At

I have a RichEditBox for which I want to turn the TextPrediction on and off while the user is typing. This is the code I am trying but its just not working. It requires the keyboard to be relaunched/reopened to show the changes.

private void PostRichEditBox_SelectionChanged(object sender, RoutedEventArgs e)
{
     if(somecondition)
     {
           Searchterm.Text = mentionText;
           var scope = new InputScope();
           var inputScopeName = new InputScopeName { NameValue = InputScopeNameValue.NameOrPhoneNumber };

           scope.Names.Add(inputScopeName);

           PostRichEditBox.InputScope = scope;
           PostRichEditBox.IsTextPredictionEnabled = true;
      }
      else
      {
           var scope = new InputScope();
           var inputScopeName = new InputScopeName { NameValue = InputScopeNameValue.Chat };

           scope.Names.Add(inputScopeName);

           PostRichEditBox.InputScope = scope;

           PostRichEditBox.IsTextPredictionEnabled = false;
       }
}

I know this can be done because Rudy Huyn's 6tag does this when '@' is typed and then it shows a list of friends suggestions in place of text predictions over the keyboard. I asked him on twitter about how he does this. He just replied by changing inputmode(I think he meant inputscope). How do I do this?

1

There are 1 best solutions below

0
On

Because the prediction bar was not going away even if the predictions are turned off. It requires the keyboard to close and reopen to update the predictionBar so I have made some changes. I am now focusing on the UserControl then back to RichEditBox. And UX wise its not really noticeable as the keyboard never collapses.

if (PostRichEditBox.IsTextPredictionEnabled)
                {
                   this.Focus(Windows.UI.Xaml.FocusState.Keyboard);
                    var scope = new InputScope();
                    var inputScopeName = new InputScopeName { NameValue = InputScopeNameValue.NameOrPhoneNumber };

                    scope.Names.Add(inputScopeName);

                    PostRichEditBox.InputScope = scope;
                    PostRichEditBox.IsTextPredictionEnabled = false;
                    PostRichEditBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
                 }