{"record":{"id":"2311c8998bb6a309","repo":"dotnet/wpf","slug":"sr-stylus-pluginisduplicated","errorCode":null,"errorMessage":"SR.Stylus_PlugInIsDuplicated","messagePattern":"SR\\.Stylus_PlugInIsDuplicated","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPlugInCollection.cs","lineNumber":43,"sourceCode":"        /// Insert a StylusPlugIn in the collection at a specific index. \n        /// This method should be called from the application context only\n        /// </summary>\n        /// <param name=\"index\">index at which to insert the StylusPlugIn object</param>\n        /// <param name=\"plugIn\">StylusPlugIn object to insert, downcast to an object</param>\n        protected override void InsertItem(int index, StylusPlugIn plugIn)\n        {\n            // Verify it's called from the app dispatcher\n            _element.VerifyAccess();\n\n            // Validate the input parameter\n            if (null == plugIn)\n            {\n                throw new ArgumentNullException(nameof(plugIn), SR.Stylus_PlugInIsNull);\n            }\n\n            if (IndexOf(plugIn) != -1)\n            {\n                throw new ArgumentException(SR.Stylus_PlugInIsDuplicated, nameof(plugIn));\n            }\n\n            // Disable processing of the queue during blocking operations to prevent unrelated reentrancy\n            // which a call to Lock() can cause.\n            ExecuteWithPotentialDispatcherDisable(() =>\n            {\n                if (_stylusPlugInCollectionImpl.IsActiveForInput)\n                {\n                    // If we are currently active for input then we have a _penContexts that we must lock!\n                    ExecuteWithPotentialLock(() =>\n                     {\n                         System.Diagnostics.Debug.Assert(this.Count > 0); // If active must have more than one plugin already\n                         base.InsertItem(index, plugIn);\n                         plugIn.Added(this);\n                     });\n                }\n                else\n                {","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPlugInCollection.cs#L25-L61","documentation":"StylusPlugInCollection.InsertItem rejects a plug-in that is already in the collection, throwing ArgumentException with SR.Stylus_PlugInIsDuplicated. Each StylusPlugIn may be attached to a collection only once.","triggerScenarios":"Calling stylusPlugIns.Add(plugin) or Insert with a plug-in instance already present (IndexOf(plugIn) != -1).","commonSituations":"Re-adding the same singleton plug-in on an event (e.g. Loaded firing twice, or adding in both constructor and Loaded), or sharing one StylusPlugIn instance across several operations without tracking membership.","solutions":["Guard with IndexOf before adding: only Add when IndexOf(plugin) == -1","Track whether the plug-in was already attached (bool flag) and skip duplicate adds","Note the same instance cannot be reused across two collections of the same element; create a new instance if needed for a second element"],"exampleFix":"// before\nvoid Attach(StylusPlugIn plugin) => inkCanvas.StylusPlugIns.Add(plugin);\n// after\nvoid Attach(StylusPlugIn plugin)\n{\n    if (inkCanvas.StylusPlugIns.IndexOf(plugin) == -1)\n    {\n        inkCanvas.StylusPlugIns.Add(plugin);\n    }\n}","handlingStrategy":"validation","validationCode":"if (plugIns.IndexOf(plugin) == -1) plugIns.Add(plugin);","typeGuard":null,"tryCatchPattern":"try { plugIns.Add(plugin); } catch (ArgumentException ex) when (ex.Message.Contains(\"PlugInIsDuplicated\") || ex.ParamName == \"plugIn\") { /* already attached */ }","preventionTips":["Guard every Add with IndexOf","Track attachment state with a bool flag","Attach in exactly one lifecycle point (e.g. Loaded once)"],"tags":["wpf","stylus","duplicate"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}