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

Loading a view with a HotKey throws: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. #6431

Closed
janfokke opened this issue Aug 17, 2021 · 1 comment
Labels

Comments

@janfokke
Copy link

Describe the bug
Avalonia crashes when a new View is loaded after using a HotKey.

To Reproduce

  1. Create a new Project

  2. Add a UserControl to MainWindowView
    <UserControl Content="{Binding ViewModel}"></UserControl>

  3. Add a ViewModel Property and a function to load a new View to MainWindowViewModel.

public class MainWindowViewModel : ViewModelBase
{
    private ViewModelBase _viewModel;

    public MainWindowViewModel()
    {
        LoadView();
    }

    public ViewModelBase ViewModel
    {
        get => _viewModel;
        set => this.RaiseAndSetIfChanged(ref _viewModel, value);
    }

    public void LoadView()
    {
        ViewModel = new HotKeyViewModel(this);
    }
}
  1. Create a View containing a Button with a HotKey, like this
    <Button HotKey="f1" Command="{Binding ButtonClickCommand}" Content="{Binding SomeText}"/>

  2. Create a ViewModel for the view

class HotKeyViewModel : ViewModelBase
{
    public HotKeyViewModel(MainWindowViewModel mainWindowViewModel)
    {
        ButtonClickCommand = ReactiveCommand.Create(mainWindowViewModel.LoadView);
        SomeText = Guid.NewGuid().ToString();
    }
    private string SomeText { get; }
    public ReactiveCommand<Unit, Unit> ButtonClickCommand { get; set; }
}

Expected behavior
When the button is clicked, the LoadView function is called and the view is replaced.
The expected behavior occurs when the user clicks the button with the cursor.
But when the HotKey is used, the application crashes.

  • OS: Windows 10
  • Version 0.10.6 and prior

Additional context

Related Issues and pull requests
#5054
#5055

Exception

Unhandled exception. System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at Avalonia.Input.KeyboardDevice.ProcessRawEvent(RawInputEventArgs e) in /_/src/Avalonia.Input/KeyboardDevice.cs:line 224
   at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e) in /_/src/Avalonia.Input/InputManager.cs:line 37
   at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs:line 468
   at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs:line 33
   at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
   at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken) in /_/src/Windows/Avalonia.Win32/Win32Platform.cs:line 157
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Base/Threading/Dispatcher.cs:line 61
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 116
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 148
@janfokke janfokke added the bug label Aug 17, 2021
@janfokke
Copy link
Author

It seems to be fixed in 0.10.7

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

No branches or pull requests

1 participant