{"record":{"id":"7d5ff16114aadae3","repo":"babalae/better-genshin-impact","slug":"error-7d5ff1","errorCode":null,"errorMessage":"内部视图模型对象为空","messagePattern":"内部视图模型对象为空","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Script/Dependence/Dispatcher.cs","lineNumber":134,"sourceCode":"    /// - AutoGeniusInvokation: 启动自动七圣召唤任务\n    /// - AutoWood: 启动自动伐木任务\n    /// - AutoFight: 启动自动战斗任务\n    /// - AutoDomain: 启动自动秘境任务\n    /// </param>\n    /// <param name=\"customCt\">自定义取消令牌，允许从JS控制任务取消</param>\n    /// <exception cref=\"ArgumentNullException\"></exception>\n    /// <exception cref=\"ArgumentException\"></exception>\n    public async Task<object?> RunTask(SoloTask soloTask, CancellationToken? customCt = null)\n    {\n        if (soloTask == null)\n        {\n            throw new ArgumentNullException(nameof(soloTask), \"独立任务对象不能为空\");\n        }\n\n        var taskSettingsPageViewModel = App.GetService<TaskSettingsPageViewModel>();\n        if (taskSettingsPageViewModel == null)\n        {\n            throw new ArgumentNullException(nameof(taskSettingsPageViewModel), \"内部视图模型对象为空\");\n        }\n\n\n        CancellationToken cancellationToken;\n\n        if (customCt != null)\n        {\n            cancellationToken = customCt.Value;\n        }\n        else\n        {\n            // 如果没有自定义令牌，就使用全局令牌\n            cancellationToken = CancellationContext.Instance.Cts.Token;\n        }\n\n        // 根据名称执行任务\n        switch (soloTask.Name)\n        {","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/Dependence/Dispatcher.cs#L116-L152","documentation":"ArgumentNullException thrown by Dispatcher.RunTask at line 134 when App.GetService<TaskSettingsPageViewModel>() returns null. This is NOT a script-author error — it indicates the WPF DI container has no TaskSettingsPageViewModel registered, or the call happened before the app/DI was built or after disposal. TaskSettingsPageViewModel supplies settings (AutoWood rounds, fight strategy) needed by several task branches.","triggerScenarios":"A script runs RunTask before the WPF application finished initializing its service provider. Running the script from a unit test or headless harness that never registered TaskSettingsPageViewModel. The app is shutting down and the DI root has been disposed.","commonSituations":"Script triggered during early startup (before the main window / pages are constructed). DI registration for pages/viewmodels was removed or moved behind a condition. Integration test reused Dispatcher without spinning up the full app host.","solutions":["Gate script execution on TaskContext.Instance().IsInitialized (same check PlayMacro uses) before invoking RunTask.","Ensure TaskSettingsPageViewModel is registered in the DI container at app startup.","Do not call Dispatcher.RunTask from test/headless contexts; provide a test double or skip.","If this fires intermittently, delay script start until the main UI is loaded."],"exampleFix":"// before (C# / script host)\ndispatcher.RunTask(soloTask); // may throw if DI not ready\n\n// after (C# / script host)\nif (!TaskContext.Instance().IsInitialized)\n{\n    throw new InvalidOperationException(\"Dispatcher.RunTask requires the app to be fully initialized.\");\n}\ndispatcher.RunTask(soloTask);","handlingStrategy":"validation","validationCode":"// C# host — gate script execution on app readiness\nif (!TaskContext.Instance().IsInitialized)\n    throw new InvalidOperationException(\"Cannot run tasks before the app is fully initialized.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not start scripts until TaskContext.Instance().IsInitialized is true.","Register TaskSettingsPageViewModel in the DI container at app startup.","Do not call Dispatcher.RunTask from unit tests without a full DI host.","Delay script kickoff until the main window/pages are loaded."],"tags":["script","di","viewmodel","app-state","infrastructure"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}