【问题标题】:Categorizing properties of XAML activities对 XAML 活动的属性进行分类
【发布时间】:2010-07-12 09:42:40
【问题描述】:

使用代码活动(即由 C# 构建的活动),我们可以将类别属性添加到我们的属性中,并在工作流设计器的属性网格中很好地显示它们,例如

    [RequiredArgument]
    [Category("Input")]
    public InArgument<Guid> TermbaseId { get; set; }

在 XAML 活动中是否可以使用相同的功能?

编辑:包含的 XAML 示例:我想在以下 Xaml 中将 [Category("Input")] 添加到 In 并将 [Category("Output")] 添加到 Out

<Activity mc:Ignorable="sap" x:Class="ActivityLibrary1.Activity1" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <x:Members>
    <x:Property Name="In" Type="InArgument(x:String)" />
    <x:Property Name="Out" Type="OutArgument(x:String)" />
  </x:Members>      
<sap:VirtualizedContainerService.HintSize>240,240</sap:VirtualizedContainerService.HintSize>
  <mva:VisualBasic.Settings>Assembly references and imported namespaces for internal implementation</mva:VisualBasic.Settings>
</Activity>

【问题讨论】:

  • 你不能在 Xaml 中定义属性(你只能设置它们的值),所以我不确定你可以。您所说的“Xaml 活动”是什么意思 - 您可以发布一个示例吗?
  • @dan 你真的可以;提供的示例在编译后将在工作流中具有两个属性 In 和 Out。

标签: .net workflow-foundation-4 workflow-foundation


【解决方案1】:

应该是这样的:

<x:Property.Attributes>
    <sc:CategoryAttribute xmlns:sc="clr-namespace:System.ComponentModel;assembly=System">
        <x:Arguments><!-- x:Arguments is the Xaml way of constructing objects which require constructor arguments -->
            <x:String>Input</x:String>
        </x:Arguments>
    </sc:CategoryAttribute>
</x:Property.Attributes>

(x:参数见http://msdn.microsoft.com/en-us/library/ee795382.aspx

我不认为 CategoryAttribute 具有 TypeConverter 以获得更紧凑的表示,尽管我可能是错的......)

【讨论】:

  • 你几乎是对的,你想要的是:Input x:Property.Attributes> 只需要关闭几个标签并为该类别获取正确的命名空间。你也不需要值中的双引号:)
【解决方案2】:

您可以使用语法在 XAML 中添加属性。请参阅MSDN 文档。

【讨论】:

  • 我希望所有的 xaml 编译器都支持这个功能(比如 wpf!)
【解决方案3】:

如果它对其他人有帮助,我最终编写了一个自动属性分类器来给所有活动一个类别:

public class AutomaticPropertyCategorizer : IRegisterMetadata
{
   public void Register()
    {
        AttributeTableBuilder builder = new AttributeTableBuilder();

        foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().
           Where(a => !a.FullName.StartsWith("System")))
        {
            var activityTypes = from t in assembly.GetTypes()
                                where t.IsSubclassOf(typeof(Activity)) 
                                select t;
            foreach (Type t in activityTypes)
            {
                foreach (PropertyDescriptor pd in properties)
                {
                    if (pd.PropertyType.IsSubclassOf(typeof(InArgument)))
                    {
                        tableBuilder.AddCustomAttributes(activityType, pd.Name, new CategoryAttribute("Input"));
                    }
                    else if (pd.PropertyType.IsSubclassOf(typeof(OutArgument)))
                    {
                        tableBuilder.AddCustomAttributes(activityType, pd.Name, new CategoryAttribute("Output"));
                    }
                    else if (pd.PropertyType.IsSubclassOf(typeof(InOutArgument)))
                    {
                         tableBuilder.AddCustomAttributes(activityType, pd.Name, new CategoryAttribute("Input / Output"));
                    }
                }
            }
        }

        AttributeTable attributes = builder.CreateTable();

        MetadataStore.AddAttributeTable(attributes);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2013-02-25
    • 1970-01-01
    • 2015-01-25
    • 2011-10-11
    • 2016-07-02
    • 2010-10-31
    • 2018-05-23
    相关资源
    最近更新 更多