正如我所想,注释作为附加属性保存在工作流定义中。以下是工作流 xaml 中注释的样子:
<Sequence
xmlns:derp="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
derp:Annotation.AnnotationText="This is an annotation!">
看,就像任何其他附属财产一样。除非它不是。它是附加的工作流属性,而不是附加的DependencyProperty。这意味着它通过附加的属性服务以及Annotation 类工作。获取和设置 ModelItem 上的注释文本很简单(如下所述)。
支持注解其实并不难。只要你不介意你的 UI 看起来像垃圾。这是一个快速而肮脏的实现。
在 UI 中,添加一些用于保存和编辑注释文本的控件
<sap:WorkflowViewElement
x:Class="AnnotationSupport.MyActivityDesigner"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
xmlns:ann="clr-namespace:System.Activities.Presentation.Annotations;assembly=System.Activities.Presentation"
xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
x:Name="root"
MinWidth="100"
MinHeight="100">
<Grid Background="red">
<Grid.RowDefinitions>
<RowDefinition
Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Expander
IsExpanded="False">
<!-- HERE SHE BLOWS -->
<TextBox
Text="{Binding ModelItem.AnnotationText}" />
</Expander>
<TextBox
Grid.Row="1"
Text="{Binding ModelItem.Text}"
Margin="10" />
</Grid>
</sap:WorkflowViewElement>
每当 Annotation 文本更改时,ModelItem 的 PropertyChanged 事件就会触发,就像任何其他属性一样。如果您想从代码中获取它,最简单的方法是将 ModelItem 转换为dynamic
:
private void SetAnnotationLol(string newValue)
{
if(ModelItem != null)
((dynamic)ModelItem).AnnotationText = newValue;
}
现在,如果你想创建一个像 Fx 活动这样的漂亮用户界面......好吧......
我将由您来创建自定义Adorner来处理注释的显示和编辑。这实际上并不像最初看起来那么难。如果你还没有做,这是你的机会。