将活动序列化为xaml

将活动序列化为xaml,xaml,serialization,workflow-foundation,workflow-foundation-4,Xaml,Serialization,Workflow Foundation,Workflow Foundation 4,我在谷歌上搜索了一下,似乎找不到任何Xaml活动的例子——好的,坏的,或者其他 public static string ToXaml (this Activity activity) { // i would use ActivityXamlServices to go from Xaml // to activity, but how to go other way? documentation // is slim, and cannot infer proper

我在谷歌上搜索了一下,似乎找不到任何Xaml活动的例子——好的,坏的,或者其他

public static string ToXaml (this Activity activity)
{
    // i would use ActivityXamlServices to go from Xaml
    // to activity, but how to go other way? documentation
    // is slim, and cannot infer proper usage of 
    // ActivityXamlServices from Xml remarks :S
    string xaml = string.Empty;
    return xaml;
}
欢迎提供提示、提示和指示:)



注意:如此发现。将通过工作并在工作后更新。有谁想揍我一顿,当然。更好的是,如果你能找到一种摆脱WorkflowDesigner的方法,这似乎很奇怪,它是必需的。

好吧,就这样完成了

您可以通过以下方式将Xaml fy[即将实例转换为声明性Xaml]作为众所周知的活动:

public static string ToXaml (this Activity activity)
{
    StringBuilder xaml = new StringBuilder ();

    using (XmlWriter xmlWriter = XmlWriter.Create (
        xaml, 
        new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true, }))

    using (XamlWriter xamlWriter = new XamlXmlWriter (
        xmlWriter, 
        new XamlSchemaContext ()))

    using (XamlWriter xamlServicesWriter = 
        ActivityXamlServices.CreateBuilderWriter (xamlWriter))
    {
        ActivityBuilder activityBuilder = new ActivityBuilder 
        {
            Implementation = activity
        };
        XamlServices.Save (xamlServicesWriter, activityBuilder);
    }

    return xaml.ToString ();
}
您的Xaml可能包含某些工件,例如对System.Activities.Presentation命名空间的引用,显示为xmlns:sap=“…”。如果这在您的解决方案中出现问题,请阅读上面的源代码链接-有一种方法可以插入指令以忽略无法识别的名称空间


我会把这个打开一会儿。如果有人能找到更好的解决方案,或对此进行改进,请务必:)

基于其他解决方案(针对VS2010B2)和一些反射,我找到了针对VS2010RC的解决方案。由于
XamlWriter
在RC中是抽象的,因此序列化活动树的新方法如下:

public static string ToXaml (this Activity activity)
{
    var xamlBuilder = new StringBuilder();
    var xmlWriter = XmlWriter.Create(xamlBuilder,
        new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
    using (xmlWriter)
    {
        var xamlXmlWriter =
            new XamlXmlWriter(xmlWriter, new XamlSchemaContext());
        using (xamlXmlWriter)
        {
            XamlWriter xamlWriter =
                ActivityXamlServices.CreateBuilderWriter(xamlXmlWriter);
            using (xamlWriter)
            {
                var activityBuilder =
                    new ActivityBuilder { Implementation = sequence };
                XamlServices.Save(xamlWriter, activityBuilder);
            }
        }
    }
    return xamlBuilder.ToString();
}

如何
XamlServices.Save(文件名,活动)

这似乎不再适用于VS2010 RC。XamlWriter的构造函数现在受到保护。如果我找到了另一个解决方案,我会在这里发布。嗯,我会仔细检查我在这里发布的内容,但我非常确定我在VS2010RC中运行了这个片段
XamlWriter
是抽象的,但是在上面的代码片段中,我实例化了1)一个公共具体的
XamlXmlWriter
,2)通过公共工厂方法
ActivityXamlServices.CreateBuilderWriter
是的,这是符合VS2010RC的。您是否误读了摘要
XamlWriter
?我知道我犯了一个错误——误读了一些样本来源,并在一周内撞到了我的头@Will hrm,诚然含糊不清,但目的是避免给IO写信:S@johnnyg:不,您可以向其传递流、XmlWriter等,并且永远不要接触磁盘。