How to correctly save a WF4 ActivityBuilder

孤者浪人 提交于 2019-12-05 04:52:52

问题


I am currently saving my .NET FX 4.0.1 StateMachine activity like this:

var sb = new StringBuilder();
var xamlWriter = ActivityXamlServices.CreateBuilderWriter(
                     new XamlXmlWriter(new StringWriter(sb),
                     new XamlSchemaContext()));
XamlServices.Save(xamlWriter, activityBuilder);

return sb.ToString();

This works fine and the generated XAML looks good. Unfortunately, it is invalid. I can read it back in using ActivityXamlServices.Load but when I execute it, it says that it doesn't know the properties defined in the workflow. Opening it in the Visual Studio designer yields the same errors:

Compiler error(s) encountered processing expression "ActiveCall". "ActiveCall" is not declared. It may be inaccessible due to its protection level.

Through comparing the original XAML with the XAML produced by my code, I found out how to fix this problem. I have to have this tag before the StateMachine tag:

<mva:VisualBasic.Settings>
    Assembly references and imported namespaces for internal implementation
</mva:VisualBasic.Settings>

By the way:
The text inside the tag must be exactly like this, otherwise there will be an error when opening the WF in VS:

Failed to create a 'Settings' from the text 'FooBar'

Question:
What do I have to change in my code to have this tag in the generated XAML?


回答1:


I think I found the answer.

I have to use the following code before calling Save:

VisualBasic.SetSettings(activityBuilder, new VisualBasicSettings());

If the activityBuilder has been created from a DynamicActivity it is even better to use the following code:

VisualBasic.SetSettings(activityBuilder,
                        VisualBasic.GetSettings(dynamicActivity));

If this is not used, namespaces that are only needed for extension methods are not written to the XAML and will lead to an error when loading and executing the XAML.


I summarized my findings on code project.



来源:https://stackoverflow.com/questions/11663896/how-to-correctly-save-a-wf4-activitybuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!