stride3d/stride · error · InvalidOperationException

The EditorViewModel class can be instanced only once.

Error message

The EditorViewModel class can be instanced only once.

What it means

EditorViewModel enforces a singleton pattern per editor instance: a static Instance field tracks the one permitted instance. The constructor throws InvalidOperationException if Instance is already set, because two editor view models sharing static state would corrupt global editor state.

Solutions

  1. Reuse the existing EditorViewModel.Instance instead of constructing a new one
  2. Ensure the previous instance is torn down and Instance cleared before constructing again
  3. Restructure code so EditorViewModel is created exactly once at startup

Example fix

// before
var editor = new EditorViewModel(serviceProvider, name, versionMajor);
// after
var editor = EditorViewModel.Instance ?? new EditorViewModel(serviceProvider, name, versionMajor);
Defensive patterns

Strategy: validation

Validate before calling

if (EditorViewModel.Instance != null) return EditorViewModel.Instance;

Type guard

bool CanCreateEditor() => EditorViewModel.Instance == null;

Try / catch

try { editor = new EditorViewModel(sp, name, ver); } catch (InvalidOperationException) { editor = EditorViewModel.Instance; }

Prevention

When it happens

Trigger: Constructing EditorViewModel a second time in the same AppDomain/process, e.g. re-running editor initialization code or creating the view model in both a bootstrapper and a window.

Common situations: Unit tests constructing EditorViewModel repeatedly without resetting Instance; accidental double-initialization during app startup or editor reboot logic.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/640428e734d080c6. Report an issue: GitHub.

Appendix: source

Thrown at sources/editor/Stride.Core.Assets.Editor/ViewModel/EditorViewModel.cs:51

            serviceProvider.Get<IEditorDialogService>();

            ClearMRUCommand = new AnonymousCommand(serviceProvider, () => ClearRecentFiles());
            OpenSettingsWindowCommand = new AnonymousCommand(serviceProvider, OpenSettingsWindow);
            OpenWebPageCommand = new AnonymousTaskCommand<string>(serviceProvider, OpenWebPage);
#if DEBUG
            DebugCommand = new AnonymousCommand(serviceProvider, DebugFunction);
#endif

            MRU = mru;
            MRU.MostRecentlyUsedFiles.CollectionChanged += MostRecentlyUsedFiles_CollectionChanged;

            serviceProvider.Get<IEditorDialogService>().RegisterDefaultTemplateProviders();

            EditorName = editorName;
            EditorVersionMajor = editorVersionMajor;
            UpdateRecentFiles();
            if (Instance != null)
                throw new InvalidOperationException("The EditorViewModel class can be instanced only once.");

            Status = new StatusViewModel(ServiceProvider);
            Status.PushStatus("Ready");

            Instance = this;
        }

        private void MostRecentlyUsedFiles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UpdateRecentFiles();
        }

        /// <summary>
        /// Gets the current instance of <see cref="EditorViewModel"/>.
        /// </summary>
        public static EditorViewModel Instance { get; private set; }

        /// <summary>

View on GitHub (pinned to 96fad776d2)