Introduction To SmartParts And Workspaces (Introduction To CAB/SCSF Part 15)

本文介绍了CAB框架中智能部件和工作区的概念,解释了为何需要它们,并通过代码示例展示了如何使用这些元素。智能部件被视为CAB应用中的用户控件或其他可视元素,而工作区则是允许布局控件和智能部件的容器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

Part 14 of this series of articles discussed UIExtensionSites. This article continues our discussion of user interface elements in the Composite Application Block by giving an introduction to SmartParts, and a very brief introduction to Workspaces. Part 16 will expand on this discussion. Workspaces and SmartParts are interlinked and to understand one you need some understanding of the other.

Why do we need Workspaces and SmartParts?

We don’t have to use any of the new user interface elements described in this article. We can create a composite application as described in previous articles using just the existing Form class, UserControl class and the various visual components found in the standard toolbox in Visual Studio. Part 1 and part 5 of this series of articles showed how we can construct a very basic multiple document interface (MDI) application comprising three independent modules. Those articles didn’t use any of the CAB user interface components.

Many introductory texts on the Composite Application block start with Workspaces and SmartParts. This series of articles has left them until close to the end. This is because I personally found SmartParts very confusing (and still do to a certain extent), and because we don’t need to use them to get the benefits of the Composite Application Block. Workspaces are more useful, as we shall see.

SmartParts

‘SmartParts’ were first introduced in the original CAB code back in 2005. As mentioned above the concept can be confusing. SmartParts are most easily thought of as user controls in a CAB application. In fact they can be any visual element low-level element in your CAB user interface. They can, for example, be child forms in an MDI application.

However, in almost every case you will come across SmartParts will be user controls (including the normal children in CAB MDI applications).

Microsoft’s CAB documentation defines a SmartPart as ‘a view of data (in the MVC pattern) such as a control, a Windows Form, or a wizard page’. This is slightly misleading as SmartParts don’t necessarily have to display any data, and don’t have to be in an MVC pattern.

SmartParts Collections

In part 2 of this series of articles we saw that a WorkItem can be thought of as a ‘run-time container of components’ and that the WorkItem class has various collection classes associated with it (e.g. Items, Services, WorkItems). One of these collection classes is the SmartParts collection. As we shall see later in this article, another collection class on a WorkItem is the Workspaces collection.

So perhaps a better definition of a SmartPart is as any object that is put into the SmartParts collection in a WorkItem in the CAB.

However, even this definition is confused by the fact that the Workspaces collection itself has a SmartParts collection. This means there are two SmartParts collections at different places in any WorkItem. This article will call these ‘WorkItem SmartParts’ and ‘Workspace SmartParts’. The two collections behave in different ways, which are discussed below. Before that we’ll take a quick look at what a Workspace actually is. This brief exposition will be expanded on in part 16.

Workspaces

Microsoft’s CAB documentation defines Workspaces as:

“The components that encapsulate a particular visual layout of controls and SmartParts, such as within tabbed pages.”

Workspaces are themselves controls that allow other controls to be laid out within them. In particular they are designed to allow SmartParts to be laid out within them.

Several Workspace types are available in the Visual Studio toolbox and can just be dragged onto our screens. In this sense they are like the existing layout controls in the .NET Framework (e.g. the TabControl).

Where Workspaces differ from existing layout controls is in the fact that they are part of and utilize the CAB dependency injection containers (Workitems). As with SmartParts, WorkItems have a ‘Workspaces’ collection which, obviously, contains all the Workspaces associated with the WorkItem.

We can create a Workspace in our Workspaces collection in the usual way with the AddNew keyword. However, we don’t need to do this if we’ve just dragged our Workspace onto a screen: the ObjectBuilder will recognize it as a Workspace when it is created, and add it to the appropriate collections.

Workspaces also interact with SmartParts in a standard way: they have a Show method that will display a SmartPart, an ActiveSmartPart property that gets a reference to the active SmartPart in the Workspace, and so on. These methods will be examined in more detail in part 16 of this series of articles.

SmartParts Collections: WorkItem SmartParts

We look now at the SmartParts collections in more detail. To show the concepts involved there is a code example available.

The SmartParts collection in a WorkItem is just a filter on the Items collection, filtering for any Items where the underlying class is decorated with the attribute ‘SmartPart’. If we have an object whose class is decorated with the SmartPart attribute and add it directly to either the Items collection or the SmartParts collection it will appear in both. However, if we try adding an object NOT decorated with the attribute to the SmartParts collection it will get added to the Items collection only.

The underlying type of our SmartPart doesn’t matter for this . It can be any class at all. As long as that class is decorated with ‘SmartPart’ it will behave as described above. The intention is that objects added to this collection be visual ones (user controls, forms etc.) but they don’t have to be.

Example of WorkItem SmartParts

This is illustrated in the code example. Here we have a user control, UserControl1, that is decorated with the SmartPart attribute:

    [SmartPart]
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    }

In the AfterShellCreated method we add this to the SmartParts collection of the RootWorkItem:

RootWorkItem.SmartParts.AddNew<UserControl1>();

We then examine what effect that has on the various collection classes on the RootWorkItem. We can see that the UserControl1 object is in both the SmartParts collection and the Items collection as we would expect (see below for the results output).

Similarly we define a plain old .NET class and also decorate that with the SmartPart attribute:

    [SmartPart]
    internal class TestClass {}

We then just add that to the Items collection of the RootWorkItem in AfterShellCreated:

RootWorkItem.Items.AddNew<TestClass>();

If we now look at the RootWorkItem collection classes we see that the TestClass object is in both the SmartParts collection and the Items collection in exactly the same way as the UserControl instance above. It is a ‘SmartPart’ even though it has no visual element.

The full output of the relevant collections from this example is shown below under ‘Output from SmartParts Example’.

SmartParts Collections: Workspace SmartParts

As discussed above, the second SmartParts collection is on a Workspace. An object gets added to this collection if we call the Show method on the Workspace with the object as the parameter.

For the Workspace SmartParts collection it doesn’t matter whether the object’s class is decorated with the ‘SmartPart’ attribute or not: it gets added to the Workspace SmartParts collection when the Show method of the Workspace is called regardless.

Note that the Show method doesn’t add the object into the Items collection of the WorkItem, nor does it add the object into the WorkItem SmartParts collection.

We’ll examine how Workspaces work, and how they interact with SmartParts, in more detail in part 16 of this series of articles. In particular we’ll look at why the Show method is a useful one, and we’ll examine how SmartPartInfo classes work and why we need them. For now you can just accept that this second collection exists and can be useful.

Issues with Workspace SmartParts

One issue here is that objects can only be added to the SmartParts collection of a Workspace if they inherit System.Windows.Forms.Control at some level. That is, in this case our SmartParts are forced to be visual controls.

Also you can’t add SmartParts directly to the SmartParts collection of a Workspace. The only way to add an object to this collection is to use the Show method of the Workspace (I think). Note that they don’t get removed from this collection just because you call Show with a different SmartPart.

You were warned that this is confusing.

Example of Workspace SmartParts

The code example discussed above has been extended to show how Workspace SmartParts work. We have added a DeckWorkspace (one of the more common Workspace classes) to our Shell form simply by dragging it from the Toolbox. We’ve also added another user control class to the project, UserControl2.

We then display the user control in the Workspace by calling the Show method:

this.Shell.deckWorkspace1.Show(new UserControl2());

This has the effect of adding UserControl2 to the SmartParts collection of the Workspace. However, UserControl2 is NOT added to the Items collection of the WorkItem, nor to the SmartParts collection of the WorkItem.

In the example above we defined a plain old .NET class and added it to the WorkItem’s SmartParts collection. We can’t do this with the Workspace’s SmartParts collection because this collection will only accept objects that inherit from Control at some level.

One thing to note here is that UserControl2 is decorated with the SmartPart attribute, but this makes no difference at all to the example. We can remove it and the example will work in the same way. This is because this attribute is not relevant for the SmartParts collection on a Workspace.

Output from SmartParts Example

The SmartParts example uses some simple looping code to output the collections created to the Output window. The results of this are shown below. As we can see, UserControl1 and TestClass (discussed in the section ‘Example of WorkItem SmartParts’ above) appear in the Items collection, and in the SmartParts collection at RootWorkItem level. However, UserControl2 (from the section ‘Example of Workspace SmartParts’) appears ONLY in the SmartParts collection on the Workspace. This is what we’d expect as discussed above.

ITEMS:
[d506f65b-e818-4379-b15a-5e53bfe7777f, Microsoft.Practices.CompositeUI.State]
[f4d17dcd-13cc-4d61-b24b-0b3cd03c88a8, Shell.Form1, Text: Form1]
[deckWorkspace1, Microsoft.Practices.CompositeUI.WinForms.DeckWorkspace]
[1d141251-3436-4a8a-8bd3-1e63ceca3d9e, Shell.UserControl1]
[632482f5-aaa3-490b-b800-3575bea33a06, Shell.TestClass]
SMARTPARTS:
[1d141251-3436-4a8a-8bd3-1e63ceca3d9e, Shell.UserControl1]
[632482f5-aaa3-490b-b800-3575bea33a06, Shell.TestClass]
WORKSPACES:
[deckWorkspace1, Microsoft.Practices.CompositeUI.WinForms.DeckWorkspace]
       SMARTPARTS:
       Shell.UserControl2

The SmartPart Attribute

Another source of confusion with SmartParts is what exactly the SmartPart attribute does. Several texts mention that it doesn’t seem to make much difference whether it’s there or not, and this is true to an extent with the Workspace SmartParts collection.

In fact there are (at least) two places where the SmartPart attribute makes a difference:

  1. As we’ve already seen any object decorated with the SmartPart attribute that is added to either the Items or the SmartParts collection of a WorkItem will effectively be added to both collections. Any object without the SmartPart attribute added to either collection will end up in the Items collection only. Remember that the SmartParts collection of a WorkItem is really just a filter on the Items collection for any objects that have the SmartPart attribute.
  2. The SmartPart attribute also makes a difference if we add a Control to the Controls collection of the Shell, or to the Controls collection of a Control on the Shell, when the Shell is being initialized (in its constructor). In this case the ObjectBuilder recognizes that we have a WorkItem SmartPart. It adds the SmartPart to the Items collection of the WorkItem and hence to the SmartParts collection (since it has the SmartPart attribute).

This is still confusing, but the behaviour in 2 is there so that if you drag a User Control with the SmartPart attribute onto the Shell from the Toolbox then it will get added to the SmartParts collection of the root WorkItem at start up.

Example of Effects of SmartPart Attribute on User Controls Dragged on to the Shell Form

Some example code showing this is available. This has a user control with the SmartPart attribute applied (SmartPartUserControl) and a user control with no SmartPart attribute (NormalUserControl). Both of these have been dragged onto the Shell form (Form1).

Additionally in the constructor of the Shell form we manually instantiate a SmartPartUserControl and add it to the Form’s Controls collection:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SmartPartUserControl smartPartUserControl = new SmartPartUserControl();
            smartPartUserControl.Name = "Manually instantiated user control";
            this.Controls.Add(smartPartUserControl);
        }
    }

That’s all the code that’s been added. Once again the code dumps the various relevant WorkItem collections in AfterShellCreated by using simple loops outputting to the Output window:

ITEMS:
[fa70dbe9-0c30-443e-9fe6-a13e353994e0, Microsoft.Practices.CompositeUI.State]
[d67d7177-cf63-4b4f-9447-ef2fd1e8271b, Shell.Form1, Text: Form1]
[userControl11, Shell.SmartPartUserControl]
[deckWorkspace1, Microsoft.Practices.CompositeUI.WinForms.DeckWorkspace]
[Manually instantiated user control, Shell.SmartPartUserControl]
SMARTPARTS:
[userControl11, Shell.SmartPartUserControl]
[Manually instantiated user control, Shell.SmartPartUserControl]
WORKSPACES:
[deckWorkspace1, Microsoft.Practices.CompositeUI.WinForms.DeckWorkspace]
       SMARTPARTS:

As we can see, both SmartPartUserControls have been added to both the Items and WorkItem SmartParts collections, whilst the NormalUserControl does not appear (although we can see it on the Shell fine: it just hasn’t been added to these collections). This is what we’d expect from the discussions above.

Summary of the SmartParts Collections and their Intention

The intention of the Workspace SmartParts is to give us (and the CAB Framework) a collection of SmartParts that have been shown in a Workspace but not closed. These can be re-activated (effectively brought to the front). We can show Controls that are not marked with the SmartPart attribute in a Workspace (and hence arguably aren’t really SmartParts). However this collection needs to track everything that has been shown, and so it all ends up in this collection.

The intention of the WorkItem SmartParts is to give us a collection of SmartParts that we as developers can control and use. Here we do want everything in the collection to have the SmartPart attribute: this is what distinguishes SmartParts from ordinary Items as far as this collection is concerned.

Workspace SmartParts have to be Controls (i.e. inherit from System.Windows.Forms.Control at some level). WorkItem SmartParts can be any object.

My personal opinion is that it would have been less confusing if the CAB developers had given the SmartParts collection on the Workspace a different name: ‘ViewsShown’ or something similar might have been better.

Conclusion

This article gave a brief introduction to SmartParts and showed why they can be confusing. It also gave an even briefer introduction to Workspaces. Part 16 of this series of articles will examine Workspaces in more detail, and expand on how we can use SmartParts in our code. It will also look at the SmartPartInfo class.

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值