Skip to content

Commit 79d5497

Browse files
Connect PlatformBehavior to XamlRoot.Content (#32)
1 parent 7e86d93 commit 79d5497

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

src/Plugin.Maui.KeyListener/KeyboardBehavior.Windows.cs

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,45 @@
11
#if WINDOWS
22
using Microsoft.UI.Xaml;
33
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;
94

105
namespace Plugin.Maui.KeyListener;
116

127
public partial class KeyboardBehavior : PlatformBehavior<VisualElement>
138
{
14-
UIElement? _content;
15-
169
protected override void OnAttachedTo(VisualElement bindable, FrameworkElement platformView)
1710
{
1811
base.OnAttachedTo(bindable, platformView);
1912

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)
2214
{
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;
2817
}
2918
}
3019

3120
protected override void OnDetachedFrom(VisualElement bindable, FrameworkElement platformView)
3221
{
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+
}
4427

45-
void OnWindowKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
46-
{
47-
Console.WriteLine($"OnWindowKeyDown {e.Key}");
28+
base.OnDetachedFrom(bindable, platformView);
4829
}
4930

50-
void OnKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
31+
void OnKeyDown(object sender, KeyRoutedEventArgs e)
5132
{
5233
var eventArgs = e.ToKeyPressedEventArgs();
53-
this.RaiseKeyDown(eventArgs);
34+
RaiseKeyDown(eventArgs);
5435
if (eventArgs.Handled)
5536
e.Handled = true;
5637
}
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-
}
6638

67-
void OnKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
39+
void OnKeyUp(object sender, KeyRoutedEventArgs e)
6840
{
6941
var eventArgs = e.ToKeyPressedEventArgs();
70-
this.RaiseKeyUp(eventArgs);
42+
RaiseKeyUp(eventArgs);
7143
if (eventArgs.Handled)
7244
e.Handled = true;
7345
}

0 commit comments

Comments
 (0)