4

我用谷歌搜索了一下,似乎找不到任何 Xaml-fying 活动的例子——好、坏或其他!

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 的方法,这似乎很奇怪。

4

3 回答 3

4

好的,所以通过这个论坛发帖工作。

您可以通过 Xaml-fy [即将实例转换为声明性 Xaml] 一个众所周知的 Activity

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="..."。如果这在您的解决方案中出现问题,请阅读上面的源链接 - 有一种方法可以注入指令来忽略无法识别的命名空间。

将把它打开一段时间。如果有人能找到更好的解决方案,或对此进行改进,请务必:)

于 2010-02-25T20:35:05.670 回答
3

怎么样XamlServices.Save(filename, activity)

于 2010-12-31T03:00:51.510 回答
0

Based on the other solution (for VS2010B2) and some Reflectoring, I found a solution for VS2010RC. Since XamlWriter is abstract in the RC, the new way to serialize an activity tree is this:

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();
}
于 2010-03-24T19:11:03.500 回答