Browse Source

Fix crash when KeyBindings change while they are being handled

Fusion86 5 years ago
parent
commit
3304d646c0
1 changed files with 7 additions and 1 deletions
  1. 7 1
      src/Avalonia.Input/KeyboardDevice.cs

+ 7 - 1
src/Avalonia.Input/KeyboardDevice.cs

@@ -211,12 +211,18 @@ namespace Avalonia.Input
                         {
                             var bindings = (currentHandler as IInputElement)?.KeyBindings;
                             if (bindings != null)
-                                foreach (var binding in bindings)
+                            {
+                                // Create a copy of the KeyBindings list.
+                                // If we don't do this the foreach loop will throw an InvalidOperationException when the KeyBindings list is changed.
+                                // This can happen when a new view is loaded which adds its own KeyBindings to the handler.
+                                var cpy = bindings.ToArray();
+                                foreach (var binding in cpy)
                                 {
                                     if (ev.Handled)
                                         break;
                                     binding.TryHandle(ev);
                                 }
+                            }
                             currentHandler = currentHandler.VisualParent;
                         }