WF4.0实战(十一):邮件通知
在工作流开发中,邮件通知是必不可少。这篇文章中,我将是使用WF4.0一步一步打造一个功能完整的邮件通知节点。
首先,新建一个WorkflowConsoleApplication项目,改名为MailNoticeDemo,如下图:
添加一个CodeActivity活动命名为MailNotice,添加一个ActivityDesigner活动命名为MailNoticeDesigner。项目结构如下图:
MailNotice用于写发送邮件的业务逻辑,MailNoticeDesigner用于设计活动的界面,现在MailNotice和MailNoticeDesigner是没有任何关联的,我们在MailNotice类上添加[Designer(typeof(MailNoticeDesigner))],关联MailNotice和MailNoticeDesigner,还需要引入System.ComponentModel命名空间,代码如下。
using System.Activities; using System.ComponentModel; using System.Activities.Presentation.Metadata; using System.Activities.Presentation.PropertyEditing; using System; namespace MailNoticeDemo { [Designer(typeof(MailNoticeDesigner))] public sealed class MailNotice : CodeActivity {
………
}
这时,MailNotice和MailNoticeDesigner活动中还没有进行编码。在项目中引入一个邮件发送类MailHelper.cs,编写MailNotice代码,如下:
- [Designer(typeof(MailNoticeDesigner))]
public sealed class MailNotice : CodeActivity
{
public InArgument<string> Receive { get; set; }
public InArgument<string> Subject { get; set; }
public InArgument<string> Content { get; set; }
public string Attachment { get; set; }
static MailNotice()
{
AttributeTableBuilder builder = new AttributeTableBuilder();
builder.AddCustomAttributes(typeof(MailNotice), "Attachment", new EditorAttribute(typeof(FilePickerEditor), typeof(DialogPropertyValueEditor)));
MetadataStore.AddAttributeTable(builder.CreateTable());
}
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override void Execute(CodeActivityContext context)
{
SMTP smtp = new SMTP("你邮箱地址·", "显示的名称", new string[] { Receive.Get(context) }, null, null, Subject.Get(context), Content.Get(context), new string[] { Attachment }, "邮件发送服务", 25, "你邮箱地址·", "你邮箱密码", false);
try
{
smtp.Send();
}
catch (Exception ex)
{
string error = ex.Message;
if (ex.InnerException != null)
{
error = ex.InnerException.Message;
}
Console.WriteLine("邮箱发送失败: " + error);
}}
}
设计MailNoticeDesigner活动的UI,设计UI之前让你可以先了解一下ExpressionTextBox。详见:expressiontextbox-101
通过expressiontextbox和WPF的控件,设计UI界面如下:
Xaml代码如下:
<sap:ActivityDesigner x:Class="MailNoticeDemo.MailNoticeDesigner"
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:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation"
xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="221" d:DesignWidth="336">
<sap:ActivityDesigner.Resources>
<ResourceDictionary x:Uid="ResourceDictionary_1">
<sapc:ArgumentToExpressionConverter x:Uid="sadv:ArgumentToExpressionConverter_1" x:Key="ArgumentToExpressionConverter" />
</ResourceDictionary>
</sap:ActivityDesigner.Resources>
<Grid Height="190" Width="328">
<Grid.RowDefinitions>
<RowDefinition Height="28"></RowDefinition>
<RowDefinition Height="28"></RowDefinition>
<RowDefinition Height="54"></RowDefinition>
<RowDefinition Height="47" />
<RowDefinition Height="31*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".2*"></ColumnDefinition>
<ColumnDefinition Width=".8*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="收件人" Height="28" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Name="label1" VerticalAlignment="Top" />
<sapv:ExpressionTextBox Grid.Row="0" Grid.Column="1"
OwnerActivity="{Binding Path=ModelItem}"
Expression="{Binding Path=ModelItem.Receive, Mode=TwoWay, Converter={StaticResource ArgumentToExpressionConverter}, ConverterParameter=In }"
UseLocationExpression="False" />
<Label Content="主题:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" Name="label2" VerticalAlignment="Top" />
<sapv:ExpressionTextBox Grid.Row="1" Grid.Column="1"
OwnerActivity="{Binding Path=ModelItem}"
Expression="{Binding Path=ModelItem.Subject, Mode=TwoWay, Converter={StaticResource ArgumentToExpressionConverter}, ConverterParameter=In }"
UseLocationExpression="False" />
<Label Content="正文:" Grid.Row="2" HorizontalAlignment="Left" Name="label3" VerticalAlignment="Top" />
<sapv:ExpressionTextBox Grid.Row="2" Grid.Column="1"
OwnerActivity="{Binding Path=ModelItem}"
Expression="{Binding Path=ModelItem.Content, Mode=TwoWay, Converter={StaticResource ArgumentToExpressionConverter}, ConverterParameter=In }"
UseLocationExpression="False" Margin="0,0,0,1" Grid.RowSpan="2" />
</Grid>
</sap:ActivityDesigner>
- 这样我们就完成了这个邮件活动。测试一下。在Workflow1中拖入这个活动。输入收件人、主题、正文,如下图:
- 设置附件,如下图:
- 启动工作流发送邮件:
WorkflowInvoker.Invoke(new Workflow1());
你会发现这个活动的图标不够美观,让我们修改一下这个自定义活动的图标。 在MailNoticeDesigner.xaml中加入下面代码
- <sap:ActivityDesigner.Icon>
<DrawingBrush>
<DrawingBrush.Drawing>
<ImageDrawing>
<ImageDrawing.Rect>
<Rect Location="0,0" Size="16,16" ></Rect>
</ImageDrawing.Rect>
<ImageDrawing.ImageSource>
<BitmapImage UriSource="mail.ico" ></BitmapImage>
</ImageDrawing.ImageSource>
</ImageDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</sap:ActivityDesigner.Icon>
设置图标mail.ico,将Build action设置为Resource,如下图:
看一下我们最终打造的邮件活动:
总结:这篇文章详细讲解了创建WF4.0一个自定义活动的完整过程,虽然很简单,但对创建自定义活动很有参考价值。
WF4.0实战(十一):邮件通知相关推荐
- WF4.0实战系列索引
从WF4.0 betal1出来的时候就开始使用WF4.0,由于资料不多,学习过程也非常艰苦.今年四月份的时候打算写WF4.0实战系列,由于今年是本命年故坚持写了24篇文章.这个系列的文章都有一个特点, ...
- WF4.0实战(十):分布式酒店订房系统
这篇文章主要是实现一个分布式的酒店订房功能.主要阐述如何通过WCF加WF实现一个分布式系统模型. 这个Demo的场景说明: 一家酒店将房间信息存储在SQL Server数据库中,酒店的工作人员根据客户 ...
- WF4.0实战(四):博客申请流程
概述: 我是两年前申请的博客园.如今仍然记得很清楚,与现在的方式有点不同,当时注册也是要申请的,现在是注册不需要申请,而注册之后,开博需要申请.当时感觉有点新鲜,同样也感到欣慰,有如此敬业的管理员已经 ...
- WF4.0实战(六):控制WPF动画
这个例子改造了王晓冬老师的:用WF流程控制WPF动画. 本文用一个小例子演示了在WF中定义两个操作步骤,用来控制WPF页面元素的动画.王冬老师当时使用的是WF3.0,现在我改成WF4.0. 先看效果: ...
- WF4.0实战(七):请假流程(带驳回操作)
我使用WF4.0有很长一段时间了,但是对WF3.0和WF3.5自知甚少,对状态机也不甚了解.今天生鱼片前辈的博文:WF4实现工作流驳回流转模型的几种设计方案 中提出的四中实现驳回的方式中.第一种大家都 ...
- WF4.0实战(九):猜数字游戏,测下你的智力
今天周末,用WF4.0写个小游戏,供大家娱乐一下.界面做的不是很美观,请见谅. 效果: 一运行程序,游戏就开始了. 你输入一个数字4,提示"尝试输入一个较大的数字",如下图: 你输 ...
- WF4.0实战(十五):伤心聊天室
大家都知道,一般能使用WCF的Callback Contract能实现聊天室.这篇文章我将使用WF4.0是实现我的伤心聊天室.先看效果,再讲如何实现和使用WF4.0的优势,最后总结.界面很简洁,请见谅 ...
- WF4.0实战(二):超市收银软件
今天翻到了伍迷前辈的大话设计模式中的<第二章 商场促销-策略模式>.我感觉用WF去实现,比较简单直观,我很喜欢做简单的事情.故使用了伍迷前辈书中的两个主要人物小菜和大鸟,写下这篇博客. 时 ...
- WF4.0实战(一):文件审批流程
http://www.cnblogs.com/zhuqil/archive/2010/04/13/DocumentApprovalProcess.html 转载于:https://www.cnblog ...
最新文章
- Laravel7使用Auth进行用户认证
- Java中 实现通过文件夹选择任一图像,从而进行图像卷积操作
- 微信支付(JSAPI) - chooseWXPay fail 问题解决
- linux数据流重定向
- Angular和SAP C4C的事件处理队列 1
- ue4缓存位置怎么改_怎么从蓝图节点跳转到C++源码?
- oc之Mac-响应链(Responder Chain)
- WebSocket之JS发送二进制
- java 比较源文件_Beyond Compare比较Java源代码文件的详细操作方法
- 【Python】爬虫爬取各大网站新闻(一)
- HLS 开发学习(五) 稀疏矩阵向量乘法
- 【自动驾驶】高级驾驶辅助系统(ADAS)
- 研究生毕业论文如何选题
- 解决active样式在ios手机上没有生效的问题
- java飘落的雪花_[Java教程]树叶飘落、雪花飘落等同时多个图片飘落
- 【Android Dialog】Dialog
- 诺奖以上,真相未满:追捕黑洞二百年
- python算法教程百度云_如何用免费GPU学习AI算法?这篇算法资源大集锦别错过
- 自己写的C盘清理工具 Ver1.0.0
- 微信小程序视频点播在线视频学习系统 毕业设计 课程设计(5)视频播放页面