重新托管工作流设计器默认导入的命名空间

发布于 2024-11-09 15:30:01 字数 1008 浏览 2 评论 0原文

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

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

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

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

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


以下解决方案的其他信息

为了解决此问题,我创建了所需的“全新”活动 XAML 文件,将其添加到我的项目中,将其构建操作设置为嵌入式资源及其自定义工具为空字符串。

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

_Wd = new WorkflowDesigner();

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

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

I'm trying to rehost the WF4 Workflow Designer. In the "Imports" tab, I'd like to have some namespaces imported by default. It looks like this:

Imported namespaces http://imageshack.us/m/850/5383/imports.png

After a lot of research, I figured out that if you look at

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

you'll get to see things that are already imported. However, adding a namespace manually to this collection does not seem to have any effect. My question, therefore, is: How do I add imported namespaces to this list the right way? Or, how do I get the context to refresh using my manually added namespace imports?


Additional information to the solution below:

In order to solve this, I created my desired "clean slate" activity XAML file, added it to my project, set its Build Action to Embedded Resource and its Custom Tool to empty string.

Then, in the code that initializes my WorkflowDesigner, I do the following:

_Wd = new WorkflowDesigner();

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

Now my work flow has all the desirable namespaces imported.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落日海湾 2024-11-16 15:30:01

我这样做的方法不是从完全空的工作流程开始,而是创建一个包含所需导入的空模板。添加如下内容:

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

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

The way I do this is by not starting with a completely empty workflow but creating an empty template with the required imports. Add something like:

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

to the root activity in the XAML file to import System.IO

听闻余生 2024-11-16 15:30:01

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

症状是您在设计器主机中出现一个模式对话框,该对话框在 System.Activities.Presentation.View.ImportDesigner.OnContextChanged() 中声明空引用异常,
如果您加载的活动

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 访问 .Collection “Imports”属性引发空指针异常。可能是因为没有加载有关导入名称空间的信息。

Activity

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;
  }
}

Your solution solves a different problem with rehosting stream based activities as well and there is no proposed solution that I could find, so I post this here.

The Symptom is that you get a modal dialog in your designer host, that states a null reference exception in System.Activities.Presentation.View.ImportDesigner.OnContextChanged(),
if you loaded an activity with

ActivityXamlServices.Load(aStream)  // wrong way!

Cause: In

OnContextChanged() // of class ImportDesigner

in https://referencesource.microsoft.com/#System.Activities.Presentation/System.Activities.Presentation/System/Activities/Presentation/View/ImportDesigner.xaml.cs,1d24713ba95e69c5 accessing the .Collection of the "Imports" property raises the null pointer exception. Probably because no information on imported namespaces got loaded.

Solution: Use the Activity

ab.Implementation // of the ActivityBuilder ab 

from Alex' post and .Load() it into the WorkflowDesigner instance.

Full code snippet:

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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文