Click here to Skip to main content
15,894,460 members
Articles / Desktop Programming / XAML

WF4 Custom activities for message mediation

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
20 Dec 2012CPOL24 min read 108.7K   2K   72  
This article describes a design, implementation, and usage of custom message mediation activities for a XAML workflow.
//*****************************************************************************
//    Description.....WF4 Message Mediation Library
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright © 2009 ATZ Consulting Inc. (see included license.rtf file)         
//                        
//    Date Created:    07/07/09
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    07/07/09    Roman Kiss     Initial Revision
//    10/10/12    Roman Kiss     Upadate 4.5
//*****************************************************************************
//
#region Namespaces
using System;
using System.Activities;
using System.Activities.Presentation.View;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml.Linq;
using Microsoft.VisualBasic.Activities;
//
using RKiss.MessageMediationActivityLibrary.Design.Dialogs.Forms;
#endregion

namespace RKiss.MessageMediationActivityLibrary.Design
{
    // Interaction logic for CreateMessageDesigner.xaml
    public partial class CreateMessageDesigner
    {
        public CreateMessageDesigner()
        {
            InitializeComponent();   
        }

        //protected override void OnModelItemChanged(object newItem)
        //{
        //    base.OnModelItemChanged(newItem);
        //    this.ExpandState = false;
        //}

        protected override void OnContentTemplateChanged(DataTemplate oldContentTemplate, DataTemplate newContentTemplate)
        {
            base.OnContentTemplateChanged(oldContentTemplate, newContentTemplate);
        }

        private void buttonMediator_Click(object sender, RoutedEventArgs e)
        {
            try
            {    
                // get the etb from dynamicly datatemplate
                ContentControl cc = e.Source as ContentControl;
                var sp = VisualTreeUtils.GetNamedChild<StackPanel>(cc.Parent, "stackPanel", 5);
                ExpressionTextBox expMediator = sp.Children[0] as ExpressionTextBox;
                if (expMediator == null)
                    throw new Exception("Internal error, ExpressionTextBox 'expMediator' doesn't exist in the content");
         
                var sourceUri = ModelItem.Properties.FirstOrDefault(p => p.Name == "SourceUri");
                string xmltext = expMediator.Expression == null ? string.Empty : (string)expMediator.Expression.Properties["ExpressionText"].ComputedValue;

                CreateMessageForm dialog = new CreateMessageForm(xmltext, sourceUri == null ? string.Empty : (string)sourceUri.ComputedValue);
                dialog.ShowDialog();
                if (dialog.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    // set value for Mediator property
                    if (string.IsNullOrEmpty(dialog.XmlText) && expMediator.Expression != null)
                    {
                        ModelItem.Properties["Mediator"].SetValue(null);
                    }
                    else if (string.IsNullOrEmpty(dialog.XmlText) == false)
                    {                   
                        string xmltext2 = XDocument.Parse(dialog.XmlText, LoadOptions.None).ToString(SaveOptions.None);
                        Argument arg = Argument.Create(typeof(XElement), ArgumentDirection.In);
                        arg.Expression = new VisualBasicValue<XElement> { ExpressionText = xmltext2 };
                        ModelItem.Properties["Mediator"].SetValue(arg);
                    }

                    this.ModelItem.Properties["MessageVersion"].SetValue(dialog.MessageVersion); 
                    this.ModelItem.Properties["SourceUri"].SetValue(dialog.SourceUri);
                    ((RoutedCommand)DesignerView.CommitCommand).Execute(2, expMediator);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions