Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top