5

我正在 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。我删除了它,然后按照以下步骤创建了一个新活动:

  1. 右键单击项目并选择添加>新项目...

  2. 在 Add New Item 对话框窗口中,导航到Installed > Visual C# Items > Workflow > Activity

  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>
4

2 回答 2

6

我能够弄清楚这个问题。

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

所以,我对此有点不高兴,但我决定再次破解 XAML 代码,因为在完成 WF 教程时,我在设计器中肯定有一个活动可以接受 C# 中的表达式。我打开了那个项目并浏览了 XAML 代码。

就在那时,我注意到了这一行:

<sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>

我搜索了 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>

所以,有点不同(对我来说,无论如何),但我现在可以使用 C#,所以我很高兴。

于 2012-05-31T19:47:50.233 回答
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 的新增功能

于 2012-06-01T01:43:58.270 回答