在工作流设计器中从VB切换到C#

Spe*_*r R 5 xaml workflow-activity visual-studio workflow-foundation-4

我正在Visual Studio 11 Beta中创建一个活动库(尽管我已经在VS2010中重复了我的所有步骤,结果相同),目标是.NET 4.0框架.

当我开始通过工作流设计器输入参数时,我注意到默认值框中的" 输入VB表达式 "消息.我不确定如何将语言上下文从VB更改为C#.

要创建项目,我按照以下步骤操作:

  1. 转到文件 > 新建,然后选择项目...

  2. 在New Project对话框窗口的Installed > Templates部分中,选择Visual C# > Workflow > Activity Library

  3. 像往常一样命名项目,然后单击" 确定"

这基本上就是它.我注意到默认Activity1.xaml文件在默认值字段中期望VB.我删除了它,然后按照这些步骤创建一个新的Activity:

  1. 右键单击该项目,然后选择Add > New Item ...

  2. 在"添加新项"对话框窗口中,导航到" 已安装" >" Visual C#项目" >" 工作流" >" 活动"

  3. 命名活动,然后单击"确定"

结果相同,默认值字段需要VB表达式.

当我查看XAML代码时,我可以清楚地看到Microsoft.VisualBasic.Activities列出的命名空间和一个VisualBasic.Settings元素,但我不知道该如何改变它; 每次我尝试,我最终都搞砸了.这是生成的XAML代码:

<Activity mc:Ignorable="sads sap" x:Class="THINKImport.CustomerAddOrderAdd"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:local="clr-namespace:THINKImport.THINKWebReference"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities"
 xmlns:s="clr-namespace:System;assembly=System.Core"
 xmlns:s1="clr-namespace:System;assembly=System"
 xmlns:s2="clr-namespace:System;assembly=System.ServiceModel"
 xmlns:s3="clr-namespace:System;assembly=mscorlib"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:t="clr-namespace:THINKImport"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Members>
    <x:Property Name="user_login_data" Type="InArgument(local:user_login_data)" />
    <!--Removed the other properties for brevity-->
  </x:Members>
  <sap:VirtualizedContainerService.HintSize>440,440</sap:VirtualizedContainerService.HintSize>
  <mva:VisualBasic.Settings>Assembly references and imported namespaces for internal implementation</mva:VisualBasic.Settings>
</Activity>
Run Code Online (Sandbox Code Playgroud)

Spe*_*r R 5

我弄清楚了这个问题.

首先,我能够在这里找到根本原因.简而言之,它表示即使程序在C#中,也必须在表达式编辑器中使用VB.NET.

所以,我对此感到有点沮丧,但我决定对XAML代码进行另一次破解,因为在完成WF教程时,我肯定在设计器中工作的一项活动是接受C#中的表达式.我打开了那个项目并完成了XAML代码.

那时我注意到了这一行:

<sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
Run Code Online (Sandbox Code Playgroud)

我搜索了MSDN库,找到了ExpressionActivityEditor类文档.据我所知,这是.NET 4.5的新功能.在我的特定情况下,没有任何理由我不能在我的项目中使用.NET 4.5,所以我改变了它.一旦解决方案立即重新打开,所有表达式编辑器文本字段和框将接受C#.为了"重新开始",我删除了我一直在处理的活动文件并创建了一个新文件.如果有人有兴趣,这里生成的XAML代码:

<Activity mc:Ignorable="sap sap2010 sads" x:Class="THINKImport.CustomerAddOrderAdd"
      xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
      xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
      xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
      xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:THINKWebReference="clr-namespace:THINKImport.THINKWebReference">
  <x:Members>
  <x:Property Name="user_login_data" Type="InArgument(THINKWebReference:user_login_data)">
    <x:Property.Attributes>
      <RequiredArgumentAttribute />
        </x:Property.Attributes>
    </x:Property>
    <x:Property Name="customer_data" Type="InArgument(THINKWebReference:customer_data)" />
    <!--Remainder of Properties removed for brevity-->
  </x:Members>
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
  <TextExpression.NamespacesForImplementation>
    <sco:Collection x:TypeArguments="x:String">
      <x:String>System</x:String>
      <x:String>System.Collections.Generic</x:String>
      <x:String>System.Data</x:String>
      <x:String>System.Linq</x:String>
      <x:String>System.Text</x:String>
    </sco:Collection>
  </TextExpression.NamespacesForImplementation>
  <TextExpression.ReferencesForImplementation>
    <sco:Collection x:TypeArguments="AssemblyReference">
      <AssemblyReference>mscorlib</AssemblyReference>
      <AssemblyReference>System</AssemblyReference>
      <AssemblyReference>System.Core</AssemblyReference>
      <AssemblyReference>System.Data</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>
      <AssemblyReference>System.Xml</AssemblyReference>
    </sco:Collection>
  </TextExpression.ReferencesForImplementation>
  <sap2010:WorkflowViewState.IdRef>
    THINKImport.CustomerAddOrderAdd_1
  </sap2010:WorkflowViewState.IdRef>
  <sap2010:WorkflowViewState.ViewStateManager>
    <sap2010:ViewStateManager>
      <sap2010:ViewStateData Id="THINKImport.CustomerAddOrderAdd_1" sap:VirtualizedContainerService.HintSize="440,440" />
    </sap2010:ViewStateManager>
  </sap2010:WorkflowViewState.ViewStateManager>
</Activity>
Run Code Online (Sandbox Code Playgroud)

所以,有点不同(对我来说,反正)但我现在可以使用C#,所以我很开心.


Joa*_*oao 5

C#表达式

以前,工作流中的所有表达式只能用Visual Basic编写.在.NET Framework 4.5 RC中,Visual Basic表达式仅用于使用Visual Basic创建的项目.Visual C#项目现在使用C#表达式.提供了功能齐全的C#表达式编辑器,其功能包括语法高亮和智能感知.在先前版本中使用Visual Basic表达式创建的C#工作流项目将继续有效.

从Beta 1开始,C#表达式在设计时得到验证.C#表达式中的错误将标有红色波浪下划线.

更多:.NET 4.5中Windows Workflow Foundation的新功能