共用方式為


Host Provided Capabilities (WF4 EditingContext Intro Part 3)

This part 3 of my 6  part series on the EditingContext.

Introduction

Sharing Functionality between Designers 

Host provided capabilities  (you are here)

Providing callbacks for the host

Subscription/Notification engine

Inspection, Default Services and Items

EditingContext is used by our primary hosting application, Visual Studio, to provide concrete implementations of certain services.  The example that we will talk about here is the IExpressionEditorService.  Now, one thing that we would have really liked to have done in vs2010 is to provide a way to use the intellisense enabled VB editor that you see within VS inside a rehosted app.  For various reasons, we were not able to ship with that dependency in the .NET framework.  However, we needed a mechanism for our primary host to have the intellisense experience (and similarly, you could build an experience, or maybe we’ll ship one in the future for rehosted apps). 

Let’s look at the design of IExpressionEditorService:

 

Name Description
CloseExpressionEditors Closes all the active expression editors.
CreateExpressionEditor Overloaded. Creates a new expression editor.
UpdateContext Updates the context for the editing session.

Inside the ExpressionTextBox control, when the control needs to render the editor, it has code that looks like the following (note, if it can’t find an instance, it skips and just uses a plain old text box):

 if (this.Context.Services.GetService<IExpressionEditorService>() != null)
{
      return this.Context.Services.GetService<IExpressionEditorService>().CreateExpressionEditor(...)
}

Using the following overload of CreateExpressionEditor:

 IExpressionEditorInstance CreateExpressionEditor(
    AssemblyContextControlItem assemblies,
    ImportedNamespaceContextItem importedNamespaces,
    List<ModelItem> variables,
    string text
)

Now, what happens is that inside the code that we ship in the Microsoft.VisualStudio.Activities.Addin.dll, there is both a concrete implementation of this type, as well as the code which will publish an instance of this to the editing context.  Remember, this is the same thing that you can do in your app for a these host provided services.  In a subsequent post, I will get into more details of what are the ones that the designer has built in (like IExpressionEditorService).