|
1 | 1 | #if WINDOWS |
2 | 2 | using Microsoft.UI.Xaml; |
3 | 3 | using Microsoft.UI.Xaml.Input; |
4 | | -using System; |
5 | | -using System.Collections.Generic; |
6 | | -using System.Linq; |
7 | | -using System.Text; |
8 | | -using System.Threading.Tasks; |
9 | 4 |
|
10 | 5 | namespace Plugin.Maui.KeyListener; |
11 | 6 |
|
12 | 7 | public partial class KeyboardBehavior : PlatformBehavior<VisualElement> |
13 | 8 | { |
14 | | - UIElement? _content; |
15 | | - |
16 | 9 | protected override void OnAttachedTo(VisualElement bindable, FrameworkElement platformView) |
17 | 10 | { |
18 | 11 | base.OnAttachedTo(bindable, platformView); |
19 | 12 |
|
20 | | - var window = bindable.Window.Handler.PlatformView as Microsoft.UI.Xaml.Window; |
21 | | - if (bindable.Window?.Handler?.PlatformView is Microsoft.UI.Xaml.Window win && win.Content is UIElement content) |
| 13 | + if (platformView.XamlRoot?.Content is FrameworkElement content) |
22 | 14 | { |
23 | | - _content = content; |
24 | | - _content.KeyDown += OnKeyDown; |
25 | | - _content.KeyUp += OnKeyUp; |
26 | | - _content.PreviewKeyDown += OnPreviewKeyDown; |
27 | | - _content.PreviewKeyUp += OnPreviewKeyUp; |
| 15 | + content.KeyDown += OnKeyDown; |
| 16 | + content.KeyUp += OnKeyUp; |
28 | 17 | } |
29 | 18 | } |
30 | 19 |
|
31 | 20 | protected override void OnDetachedFrom(VisualElement bindable, FrameworkElement platformView) |
32 | 21 | { |
33 | | - base.OnDetachedFrom(bindable, platformView); |
34 | | - |
35 | | - if (_content is null) |
36 | | - return; |
37 | | - |
38 | | - _content.KeyDown -= OnKeyDown; |
39 | | - _content.KeyUp -= OnKeyUp; |
40 | | - _content.PreviewKeyDown -= OnPreviewKeyDown; |
41 | | - _content.PreviewKeyUp -= OnPreviewKeyUp; |
42 | | - _content = null; |
43 | | - } |
| 22 | + if (platformView.XamlRoot?.Content is FrameworkElement content) |
| 23 | + { |
| 24 | + content.KeyDown -= OnKeyDown; |
| 25 | + content.KeyUp -= OnKeyUp; |
| 26 | + } |
44 | 27 |
|
45 | | - void OnWindowKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) |
46 | | - { |
47 | | - Console.WriteLine($"OnWindowKeyDown {e.Key}"); |
| 28 | + base.OnDetachedFrom(bindable, platformView); |
48 | 29 | } |
49 | 30 |
|
50 | | - void OnKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) |
| 31 | + void OnKeyDown(object sender, KeyRoutedEventArgs e) |
51 | 32 | { |
52 | 33 | var eventArgs = e.ToKeyPressedEventArgs(); |
53 | | - this.RaiseKeyDown(eventArgs); |
| 34 | + RaiseKeyDown(eventArgs); |
54 | 35 | if (eventArgs.Handled) |
55 | 36 | e.Handled = true; |
56 | 37 | } |
57 | | - void OnPreviewKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) |
58 | | - { |
59 | | - Console.WriteLine($"OnPreviewKeyUp {e.Key}"); |
60 | | - } |
61 | | - |
62 | | - void OnPreviewKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) |
63 | | - { |
64 | | - Console.WriteLine($"OnPreviewKeyDown {e.Key}"); |
65 | | - } |
66 | 38 |
|
67 | | - void OnKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) |
| 39 | + void OnKeyUp(object sender, KeyRoutedEventArgs e) |
68 | 40 | { |
69 | 41 | var eventArgs = e.ToKeyPressedEventArgs(); |
70 | | - this.RaiseKeyUp(eventArgs); |
| 42 | + RaiseKeyUp(eventArgs); |
71 | 43 | if (eventArgs.Handled) |
72 | 44 | e.Handled = true; |
73 | 45 | } |
|
0 commit comments