At Workspace Add method, SmartPart added to RootWorkItem.SmartParts already exist

Nov 14, 2012 at 2:37 PM

public void ShowControl()
{
// get smartpart from container
var existingPart = RootWorkItem.SmartParts["MySmartPart"];

// if we called "Close" on the SmartPart, it will no longer be in the collection
if (existingPart == null)
{
Debug.WriteLine("Recreating 'MySmartPart'...");

// Addnew smartpart and ObjectFactory.DoInjections working
existingPart = RootWorkItem.SmartParts.AddNew<UserControl1>("MySmartPart");
existingPart.Name = "MySmartPart";
}

// Show it

// WorkSpace Show ->OnShow -> Add -> RootWorkItem.SmartParts.Add and twice  ObjectFactory.DoInjections working

// if there is a EventSubcription. Event invoke twice.
RootWorkItem.Workspaces[WorkspaceNames.Main].Show(existingPart);
 }

I Changed WorkSpace.cs

/// <summary>
/// Adds a SmartPart to the Workspace without showing it
/// </summary>
/// <param name="smartPart"></param>
public virtual void Add(ISmartPart smartPart)
{
if (smartPart == null) throw new ArgumentNullException("smartPart");

this.InvokeIfRequired(d =>
{
var control = smartPart as Control;
if (control == null) throw new ArgumentException("smartPart must be a Control");

if (!SmartParts.Contains(smartPart))
{
smartPart.Workspace = this;
m_smartParts.Add(smartPart);

// I added if statement
if (!RootWorkItem.SmartParts.ContainsObject(smartPart))
RootWorkItem.SmartParts.Add(smartPart, Guid.NewGuid().ToString());
this.Controls.Add(control);
}
});
 }

Coordinator
Nov 19, 2012 at 6:50 PM

Thanks for the report.  I've included this fix in my latest check-in (earlier today).