3

我正在尝试重新托管 WF4 工作流设计器。在“导入”选项卡中,我希望默认导入一些命名空间。它看起来像这样:

导入的命名空间 http://imageshack.us/m/850/5383/imports.png

经过大量研究,我发现如果你看

workflowDesigner.Context.Items.GetValue<ImportedNamespaceContextItem>().ImportedNamespaces

你会看到已经导入的东西。但是,手动向此集合添加命名空间似乎没有任何效果。因此,我的问题是:如何以正确的方式将导入的命名空间添加到此列表中?或者,如何使用手动添加的命名空间导入来刷新上下文?


以下解决方案的附加信息

为了解决这个问题,我创建了我想要的“clean slate”活动 XAML 文件,将其添加到我的项目中,将其Build Action设置为Embedded Resource并将其Custom Tool设置为空字符串。

然后,在初始化我的 WorkflowDesigner 的代码中,我执行以下操作:

_Wd = new WorkflowDesigner();

_Wd.Load(
    XamlServices.Load(
        ActivityXamlServices.CreateBuilderReader(
            new XamlXmlReader(
                Assembly.GetEntryAssembly().GetManifestResourceStream( "WpfApplication1.New.xaml" )
            )
        )
    ) as ActivityBuilder
);

现在我的工作流程已经导入了所有需要的命名空间。

4

2 回答 2

2

我这样做的方法不是从一个完全空的工作流程开始,而是创建一个带有所需导入的空模板。添加类似:

 xmlns:si="clr-namespace:System.IO;assembly=mscorlib" 

到 XAML 文件中的根活动以导入 System.IO

于 2011-05-23T15:48:22.980 回答
1

您的解决方案还解决了基于流的重新托管活动的另一个问题,并且我找不到任何建议的解决方案,因此我将其发布在这里。

症状是您在设计器主机中获得一个模式对话框,如果您使用

ActivityXamlServices.Load(aStream)  // wrong way!

原因:

OnContextChanged() // of class ImportDesigner

https://referencesource.microsoft.com/#System.Activities.Presentation/System.Activities.Presentation/System/Activities/Presentation/View/ImportDesigner.xaml.cs,1d24713ba95e69c5访问“Imports”属性的 .Collection空指针异常。可能是因为没有加载有关导入的命名空间的信息。

解决方案:使用活动

ab.Implementation // of the ActivityBuilder ab 

从 Alex 的帖子和 .Load() 到 WorkflowDesigner 实例。

完整的代码片段:

public static Activity LoadActivityFrom(FileInfo xaml)
{
  using (var rd = xaml.OpenRead())
  using (var xr = new System.Xaml.XamlXmlReader(rd))
  using (var br = System.Activities.XamlIntegration.ActivityXamlServices.CreateBuilderReader(xr))
  {
    var ab = System.Xaml.XamlServices.Load(br) as System.Activities.ActivityBuilder;
    return ab.Implementation;
  }
}
于 2017-01-23T16:38:28.647 回答