Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidOperationException when KeyBindings change while a KeyBinding is being handled #5054

Closed
Fusion86 opened this issue Nov 15, 2020 · 1 comment · Fixed by #5055
Closed

Comments

@Fusion86
Copy link
Contributor

Fusion86 commented Nov 15, 2020

AvaloniaUI throws an InvalidOperationException when currentHandler.KeyBindings changes while handling a KeyBinding in the same list. This can happen when you load a new view using a command, and this new view has a HotKey.

System.InvalidOperationException
  HResult=0x80131509
  Message=Collection was modified; enumeration operation may not execute.
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at Avalonia.Input.KeyboardDevice.ProcessRawEvent(RawInputEventArgs e)
   at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e)
   at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
   at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
   at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
   at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken)
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode)
   at BindingsInvalidOperationException.Program.Main(String[] args) in C:\Users\Wouter\source\repos\BindingsInvalidOperationException\Program.cs:line 11

I created a repo which can be used to reproduce this issue.

How to trigger the crash:

  1. Run the BindingsInvalidOperationException project
  2. Press CTRL+N
    2.1 This executes the AddTabCommand (see MainWindow.xaml -> Menu -> MenuItem -> HotKey)
    2.2 The AddTabCommand adds a new TabPageViewModel to the TabControl
    2.3 The TabControl loads a new TabPageView.xaml
    2.4 The TabPageView has two buttons with a HotKey
    2.5 I guess these new HotKeys are somehow added to the KeyBindings, which crashes the foreach loop.

An interesting fact is that it doesn't crash when you click the Add Tab MenuItem with your mouse. This is probably because the foreach loop is never executed in those cases.

Another funfact: if you click the Add Tab menu item once, so that it adds the HotKeys without crashing, you can then use CTRL+N to open more tabs and it doesn't crash. This is probably because it doesn't add the HotKeys because they already are inside the list.

I created this fix. It seems to work fine for my project, but I wouldn't know whether this is the best solution for the problem. I'll open a PR, but I'll also leave this issue open in case this solution is not acceptable.

This is all tested on 0.10.0-preview6.

@davidtimovski
Copy link

I'm experiencing this same issue on my project here. It works fine when the packages are version 0.10.0 but when I bump them up to 0.10.1 this problem occurs when using HotKeys (pressing Enter to save a new note).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants