重新承载设计器

设计器重新托管是一种常见方案,是指在自定义应用程序内托管工作流设计画布。 大多数人熟悉的托管应用程序是 Visual Studio,但是,在应用程序中显示工作流设计器可能很有用:

  • 监视应用程序(允许最终用户可视化进程以及有关进程的运行时数据,例如当前活动状态、聚合执行时间数据或有关工作流实例的其他信息)。

  • 允许用户使用有限的一组活动自定义进程的应用程序。

为了支持这些类型的应用程序,工作流设计器随附在 .NET Framework 中,并且可以托管在 WPF 应用程序内,也可以托管在具有相应 WPF 托管代码的 WinForms 应用程序中。 DesignerRehosting 示例演示:

  • 重新承载 WF 设计器。

  • 使用重新承载的工具箱和属性网格。

重新承载设计器

此示例演示如何创建 WPF 布局以包含设计器,如以下网格布局中所示(省略空间问题的工具箱代码)。 需注意包含设计器和属性网格的边框的命名。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="7*"/>
        <ColumnDefinition Width="3*"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0">
        <sapt:ToolboxControl>...</sapt:ToolboxControl>
    </Border>
    <Border Grid.Column="1" Name="DesignerBorder"/>
    <Border Grid.Column="2" Name="PropertyBorder"/>
</Grid>

接下来,该示例创建设计器,并将其主要组件 View 与用户界面中的相应容器 PropertyInspectorView 相关联。 以下示例中有一些额外的代码行值得一些解释。 需要调用 Register 以关联 .NET Framework 随附活动的默认活动设计器。 Load 被调用以传入要编辑的 WF 项。 最后, View (主画布)和 PropertyInspectorView (属性网格)放置在用户界面图面上。

protected override void OnInitialized(EventArgs e)
{
   base.OnInitialized(e);
   // register metadata
   (new DesignerMetadata()).Register();

   // create the workflow designer
   WorkflowDesigner wd = new WorkflowDesigner();
   wd.Load(new Sequence());
   DesignerBorder.Child = wd.View;
   PropertyBorder.Child = wd.PropertyInspectorView;
}

使用重新承载的工具栏

此示例在 XAML 中以声明方式使用重新托管的工具箱控件。 请注意,在代码中,可以将类型传递给 ToolboxItemWrapper 构造函数。

<!-- Copyright (c) Microsoft Corporation. All rights reserved-->
<Window x:Class="Microsoft.Samples.DesignerRehosting.RehostingWfDesigner"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sapt="clr-namespace:System.Activities.Presentation.Toolbox;assembly=System.Activities.Presentation"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="600" Width="900">
    <Window.Resources>
        <sys:String x:Key="AssemblyName">System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</sys:String>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="7*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0">
            <sapt:ToolboxControl>
                <sapt:ToolboxCategory CategoryName="Basic">
                    <sapt:ToolboxItemWrapper AssemblyName="{StaticResource AssemblyName}" >
                        <sapt:ToolboxItemWrapper.ToolName>
                            System.Activities.Statements.Sequence
                        </sapt:ToolboxItemWrapper.ToolName>
                       </sapt:ToolboxItemWrapper>
                    <sapt:ToolboxItemWrapper  AssemblyName="{StaticResource AssemblyName}">
                        <sapt:ToolboxItemWrapper.ToolName>
                            System.Activities.Statements.WriteLine
                        </sapt:ToolboxItemWrapper.ToolName>

                    </sapt:ToolboxItemWrapper>
                    <sapt:ToolboxItemWrapper  AssemblyName="{StaticResource AssemblyName}">
                        <sapt:ToolboxItemWrapper.ToolName>
                            System.Activities.Statements.If
                        </sapt:ToolboxItemWrapper.ToolName>

                    </sapt:ToolboxItemWrapper>
                    <sapt:ToolboxItemWrapper  AssemblyName="{StaticResource AssemblyName}">
                        <sapt:ToolboxItemWrapper.ToolName>
                            System.Activities.Statements.While
                        </sapt:ToolboxItemWrapper.ToolName>

                    </sapt:ToolboxItemWrapper>
                </sapt:ToolboxCategory>
            </sapt:ToolboxControl>
        </Border>
        <Border Grid.Column="1" Name="DesignerBorder"/>
        <Border Grid.Column="2" Name="PropertyBorder"/>
    </Grid>
</Window>

使用示例

  1. 在 Visual Studio 中打开DesignerRehosting.sln解决方案。

  2. 按 F5 编译并运行应用程序。

  3. 一个 WPF 应用程序启动并显示一个重新承载的设计器。