首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WF4.0 實戰(十一):打造功能完整的郵件通知自訂活動

     編輯:關於.NET

逐步建立可重用的郵件通知 CodeActivity,包含 SMTP 發送邏輯、自訂設計器 UI (ExpressionTextBox 綁定引數)、檔案選擇編輯器、自訂圖示。

專案結構

MailNoticeDemo 專案結構

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()
    {
        var builder = new AttributeTableBuilder();
        builder.AddCustomAttributes(typeof(MailNotice), "Attachment",
            new EditorAttribute(typeof(FilePickerEditor), typeof(DialogPropertyValueEditor)));
        MetadataStore.AddAttributeTable(builder.CreateTable());
    }

    protected override void Execute(CodeActivityContext context)
    {
        var smtp = new SMTP("你信箱", "顯示名稱",
            new[] { Receive.Get(context) }, null, null,
            Subject.Get(context), Content.Get(context),
            new[] { Attachment }, "SMTP伺服器", 25,
            "你信箱", "密碼", false);
        try { smtp.Send(); }
        catch (Exception ex)
        {
            Console.WriteLine("郵件發送失敗: " + (ex.InnerException?.Message ?? ex.Message));
        }
    }
}

MailNoticeDesigner XAML

<sap:ActivityDesigner x:Class="MailNoticeDemo.MailNoticeDesigner"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
    xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation">
    <sap:ActivityDesigner.Resources>
        <sapc:ArgumentToExpressionConverter x:Key="ArgumentToExpressionConverter"/>
    </sap:ActivityDesigner.Resources>
    <Grid Height="190" Width="328">
        <Grid.RowDefinitions>
            <RowDefinition Height="28"/><RowDefinition Height="28"/>
            <RowDefinition Height="54"/><RowDefinition Height="47"/>
            <RowDefinition Height="31*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".2*"/><ColumnDefinition Width=".8*"/>
        </Grid.ColumnDefinitions>
        <Label Content="收件人" Grid.Row="0" Grid.Column="0"/>
        <sapv:ExpressionTextBox Grid.Row="0" Grid.Column="1"
            Expression="{Binding Path=ModelItem.Receive, Mode=TwoWay,
                Converter={StaticResource ArgumentToExpressionConverter}, ConverterParameter=In}"
            OwnerActivity="{Binding Path=ModelItem}" UseLocationExpression="False"/>
        <Label Content="主題:" Grid.Row="1" Grid.Column="0"/>
        <sapv:ExpressionTextBox Grid.Row="1" Grid.Column="1"
            Expression="{Binding Path=ModelItem.Subject, Mode=TwoWay,
                Converter={StaticResource ArgumentToExpressionConverter}, ConverterParameter=In}"
            OwnerActivity="{Binding Path=ModelItem}" UseLocationExpression="False"/>
        <Label Content="正文:" Grid.Row="2" Grid.Column="0"/>
        <sapv:ExpressionTextBox Grid.Row="2" Grid.Column="1" Grid.RowSpan="2"
            Expression="{Binding Path=ModelItem.Content, Mode=TwoWay,
                Converter={StaticResource ArgumentToExpressionConverter}, ConverterParameter=In}"
            OwnerActivity="{Binding Path=ModelItem}" UseLocationExpression="False" Margin="0,0,0,1"/>
    </Grid>
</sap:ActivityDesigner>

測試與圖示設定

設計器 UI 效果 工作流中拖入活動設定屬性 附件檔案選擇對話框

加入圖示資源 (Build Action = Resource):

<sap:ActivityDesigner.Icon>
  <DrawingBrush>
    <DrawingBrush.Drawing>
      <ImageDrawing>
        <ImageDrawing.Rect><Rect Location="0,0" Size="16,16"/></ImageDrawing.Rect>
        <ImageDrawing.ImageSource><BitmapImage UriSource="mail.ico"/></ImageDrawing.ImageSource>
      </ImageDrawing>
    </DrawingBrush.Drawing>
  </DrawingBrush>
</sap:ActivityDesigner.Icon>
圖示 Build Action 設定 最終郵件活動外觀

原始碼:MailNoticeDemo.rar

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved