G
Guest
I'm trying to figure out how to make a SmartPart visible on my screen through
a SmartPartPlaceHolder. I have the following code (I don't have so much
understanding of CAB yet so there might be a few things wrong here and there
- I hope you can help and correct me):
//------ CABTest2 project -------------
//Program.cs
using System;
namespace CABTest2
{
static class Program
{
[STAThread]
static void Main()
{
new MainShell().Run();
}
}
}
//ProfileCatalog.xml
<?xml version="1.0" encoding="utf-8" ?>
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile" >
<Modules>
<ModuleInfo AssemblyFile="Module1.dll"/>
</Modules>
</SolutionProfile>
//MainShell.cs
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.WinForms;
namespace CABTest2
{
class MainShell : FormShellApplication<WorkItem, MainForm>
{
protected override void AfterShellCreated()
{
base.AfterShellCreated();
RootWorkItem.UIExtensionSites.RegisterSite(
"MainMenu",
Shell.MainMenuStrip);
ToolStripMenuItem l_mi = new ToolStripMenuItem("File");
RootWorkItem.UIExtensionSites["MainMenu"].Add(l_mi);
}
}
}
//MainForm.cs
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
namespace CABTest2
{
public partial class MainForm : Form
{
WorkItem m_root;
[ServiceDependency]
public WorkItem RootWorkItem
{
set
{
m_root = value;
}
}
public MainForm()
{
InitializeComponent();
}
}
}
//MainForm.Designer.cs
namespace CABTest2
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.smartPartPlaceHolder1 = new
Microsoft.Practices.CompositeUI.WinForms.SmartPartPlaceholder();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// smartPartPlaceHolder1
//
this.smartPartPlaceHolder1.BackColor =
System.Drawing.Color.Transparent;
this.smartPartPlaceHolder1.Location = new
System.Drawing.Point(12, 126);
this.smartPartPlaceHolder1.Name = "smartPartPlaceHolder1";
this.smartPartPlaceHolder1.Size = new System.Drawing.Size(219,
66);
this.smartPartPlaceHolder1.SmartPartName = "TestSmartPart";
this.smartPartPlaceHolder1.TabIndex = 1;
this.smartPartPlaceHolder1.Text = "TestSmartPart";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.smartPartPlaceHolder1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "MainForm";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private
Microsoft.Practices.CompositeUI.WinForms.SmartPartPlaceholder
smartPartPlaceHolder1;
}
}
//-------------- Module1 project -----------------
//Module1Init.cs
using Microsoft.Practices.CompositeUI;
namespace Module1
{
public class Module1Init : ModuleInit
{
WorkItem m_root;
[ServiceDependency]
public WorkItem RootWorkItem
{
set
{
m_root = value;
}
}
public override void AddServices()
{
base.AddServices();
//Add services programmatically here...
}
public override void Load()
{
base.Load();
//Display the user-interface here or perform startup custom
logic...
Module1WorkItem l_wi = m_root.WorkItems.AddNew<Module1WorkItem>();
l_wi.Run();
}
}
}
//Module1WorkItem.cs
using Microsoft.Practices.CompositeUI;
using Module1;
namespace Module1
{
public class Module1WorkItem : WorkItem
{
protected override void OnRunStarted()
{
base.OnRunStarted();
MainUserControl csp =
SmartParts.AddNew<MainUserControl>("TestSmartPart");
//MainUserControl l_uc = SmartParts.AddNew<MainUserControl>();
//Workspaces["MyMainWorkspace"].Show(l_uc);
}
}
}
//MainUserControl.cs
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
namespace Module1
{
[SmartPart]
public partial class MainUserControl : UserControl
{
public MainUserControl()
{
InitializeComponent();
}
}
}
NOTE that I also have set the SmartPartName
a SmartPartPlaceHolder. I have the following code (I don't have so much
understanding of CAB yet so there might be a few things wrong here and there
- I hope you can help and correct me):
//------ CABTest2 project -------------
//Program.cs
using System;
namespace CABTest2
{
static class Program
{
[STAThread]
static void Main()
{
new MainShell().Run();
}
}
}
//ProfileCatalog.xml
<?xml version="1.0" encoding="utf-8" ?>
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile" >
<Modules>
<ModuleInfo AssemblyFile="Module1.dll"/>
</Modules>
</SolutionProfile>
//MainShell.cs
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.WinForms;
namespace CABTest2
{
class MainShell : FormShellApplication<WorkItem, MainForm>
{
protected override void AfterShellCreated()
{
base.AfterShellCreated();
RootWorkItem.UIExtensionSites.RegisterSite(
"MainMenu",
Shell.MainMenuStrip);
ToolStripMenuItem l_mi = new ToolStripMenuItem("File");
RootWorkItem.UIExtensionSites["MainMenu"].Add(l_mi);
}
}
}
//MainForm.cs
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
namespace CABTest2
{
public partial class MainForm : Form
{
WorkItem m_root;
[ServiceDependency]
public WorkItem RootWorkItem
{
set
{
m_root = value;
}
}
public MainForm()
{
InitializeComponent();
}
}
}
//MainForm.Designer.cs
namespace CABTest2
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.smartPartPlaceHolder1 = new
Microsoft.Practices.CompositeUI.WinForms.SmartPartPlaceholder();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// smartPartPlaceHolder1
//
this.smartPartPlaceHolder1.BackColor =
System.Drawing.Color.Transparent;
this.smartPartPlaceHolder1.Location = new
System.Drawing.Point(12, 126);
this.smartPartPlaceHolder1.Name = "smartPartPlaceHolder1";
this.smartPartPlaceHolder1.Size = new System.Drawing.Size(219,
66);
this.smartPartPlaceHolder1.SmartPartName = "TestSmartPart";
this.smartPartPlaceHolder1.TabIndex = 1;
this.smartPartPlaceHolder1.Text = "TestSmartPart";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.smartPartPlaceHolder1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "MainForm";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private
Microsoft.Practices.CompositeUI.WinForms.SmartPartPlaceholder
smartPartPlaceHolder1;
}
}
//-------------- Module1 project -----------------
//Module1Init.cs
using Microsoft.Practices.CompositeUI;
namespace Module1
{
public class Module1Init : ModuleInit
{
WorkItem m_root;
[ServiceDependency]
public WorkItem RootWorkItem
{
set
{
m_root = value;
}
}
public override void AddServices()
{
base.AddServices();
//Add services programmatically here...
}
public override void Load()
{
base.Load();
//Display the user-interface here or perform startup custom
logic...
Module1WorkItem l_wi = m_root.WorkItems.AddNew<Module1WorkItem>();
l_wi.Run();
}
}
}
//Module1WorkItem.cs
using Microsoft.Practices.CompositeUI;
using Module1;
namespace Module1
{
public class Module1WorkItem : WorkItem
{
protected override void OnRunStarted()
{
base.OnRunStarted();
MainUserControl csp =
SmartParts.AddNew<MainUserControl>("TestSmartPart");
//MainUserControl l_uc = SmartParts.AddNew<MainUserControl>();
//Workspaces["MyMainWorkspace"].Show(l_uc);
}
}
}
//MainUserControl.cs
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
namespace Module1
{
[SmartPart]
public partial class MainUserControl : UserControl
{
public MainUserControl()
{
InitializeComponent();
}
}
}
NOTE that I also have set the SmartPartName