Multiplatform app, different behavior when cancelling FocusManager.LosingFocus #17654
-
I'm working on a multi-platform library (Windows, Desktop, WASM) that shows a on-screen keyboard when any TextBox get the focus. I'm using When a Key is pressed (I use Tapped event), I programmatically focus the key otherwise this.Tapped += (s, e) =>
{
Focus(FocusState.Programmatic);
KeyPressed?.Invoke(this, new KeyEventArgs(Key, IsShiftActive));
}; When the private void OnLosingFocus(object? sender, LosingFocusEventArgs args)
{
// THAT DOESN'T WORK FOR WINDOWS
// When a KeyControl gets the focus, the event has to be canceled so the TextBox doesn't lose the focus.
if ((args.NewFocusedElement is null || args.NewFocusedElement is KeyControl)
&& args.OldFocusedElement is TextBox)
{
args.Cancel = true;
return;
}
// Not sure if a Dispatcher is required here.
if (Visibility == Visibility.Visible)
{
DispatcherQueue?.TryEnqueue(() => Visibility = Visibility.Collapsed);
}
} On Window the Keyboard appears and when you press some key the TextBox do not recover the focus and after a while, when you move to another control the app simply closes, sometimes you get an unexpected error. It seems related to I've tried to change this behavior by, instead of cancelling the event, I set the focus again every time some key is pressed, but still I'm getting an unexpected error. (Some value is out of range) this.Tapped += (s, e) =>
{
KeyPressed?.Invoke(this, new KeyEventArgs(Key, IsShiftActive));
if (Keyboard.TextControl != null)
{
Keyboard.TextControl.Focus(FocusState.Programmatic);
}
}; This is the repo of the project, you can execute UnoPlatformDemo to check it. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Turned into issue #17699 |
Beta Was this translation helpful? Give feedback.
Turned into issue #17699