Click here to Skip to main content
15,893,594 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.PropertyEditing;
using System.Windows;
using System.Xml.Linq;
using Microsoft.VisualBasic.Activities;
//
using RKiss.MessageMediationActivityLibrary.Design.Dialogs.Forms;
#endregion

namespace RKiss.MessageMediationActivityLibrary.Design.Editors
{

    public class XmlValueExpressionEditor : DialogPropertyValueEditor
    {
        #region Test area
        const string _xamlTemplate2 = @" 
           <DataTemplate
                xmlns:pe='clr-namespace:System.Activities.Design.PropertyEditing;assembly=System.Activities.Design.Base'
                xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                xmlns:swdv='clr-namespace:System.Activities.Design.View;assembly=System.Activities.Design'
                xmlns:swdm='clr-namespace:System.Activities.Design.ModelTree;assembly=System.Activities.Design' 
                xmlns:sad='clr-namespace:System.Activities.Design;assembly=System.Activities.Design'
                xmlns:s='clr-namespace:System.Xml.Linq;assembly=System.Xml.Linq'
                xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
                <Grid Name='RootGrid' x:Uid='RootGrid'>
                    <Grid.Resources>
                        <swdv:ArgumentToExpressionConverter x:Key='ArgumentToExpressionConverter' />
                        <swdv:ArgumentToExpressionModelItemConverter x:Key='ArgumentToExpressionModelItemConverter' />
                        <swdm:ObjectToModelValueConverter x:Key='ObjectToModelValueConverter' />
                        <swdv:ModelPropertyEntryToOwnerActivityConverter  x:Key='ModelPropertyEntryToOwnerActivityConverter' />
                        <swdv:TypeToArgumentTypeConverter x:Key='TypeToArgumentTypeConverter' />
                        <swdv:ModelPropertyPathExpanderConverter x:Key='ModelPropertyToPathConverter' />
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width='*' />
                        <ColumnDefinition Width='Auto' />
                    </Grid.ColumnDefinitions>
                    <swdv:ExpressionTextBox Grid.Column='0' VerticalScrollBarVisibility='Auto' MinLines='1' MaxLines='4'  
                        ExpressionType='{Binding Path=ParentProperty.PropertyType, Converter={StaticResource TypeToArgumentTypeConverter}}'
                        OwnerActivity=''Binding Path=ParentProperty, Converter={StaticResource ModelPropertyToPathConverter}}'
                        PathToArgument='{Binding Path=ParentProperty, Converter={StaticResource ArgumentToExpressionConverter}}'>
                        
                                                
                    </swdv:ExpressionTextBox>
                    <pe:EditModeSwitchButton Grid.Column='1' />
                </Grid>
            </DataTemplate>";

        const string _xamlTemplate = @" 
           <DataTemplate x:Key='inlineExpressionEditorTemplate2'
                xmlns:pe='clr-namespace:System.Activities.Design.PropertyEditing;assembly=System.Activities.Design.Base'
                xmlns:swdv='clr-namespace:System.Activities.Design.View;assembly=System.Activities.Design'
                xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' 
                xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' >
                <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width='*' />
                        <ColumnDefinition Width='Auto' />
                    </Grid.ColumnDefinitions>
                    <swdv:ExpressionTextBox Grid.Column='0' />
                    <pe:EditModeSwitchButton Grid.Column='1' />
                </Grid>
            </DataTemplate>";
        #endregion

        // Methods
        public XmlValueExpressionEditor()
        {

            Type type = Type.GetType("System.Activities.Presentation.View.EditorResources, System.Activities.Presentation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
            Uri resourceLocator = new Uri(type.Assembly.GetName().Name + ";component/System/Activities/Presentation/View/EditorResources.xaml", UriKind.RelativeOrAbsolute);
            ResourceDictionary resources = (ResourceDictionary)Application.LoadComponent(resourceLocator);
            base.InlineEditorTemplate = resources["inlineExpressionEditorTemplate"] as DataTemplate;
            //base.InlineEditorTemplate = resources["ValueExpressionInlineViewTemplate"] as DataTemplate;

            #region TestArea
            //base.InlineEditorTemplate = EditorResources.GetResources()["ValueExpressionInlineViewTemplate"] as DataTemplate; // original
            try
            {
                //var xr = XmlReader.Create(new StringReader(_xamlTemplate));
                // base.InlineEditorTemplate = (DataTemplate)System.Windows.Markup.XamlReader.Load(xr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion
        }

        public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
        {
            try
            {
                if (propertyValue != null)
                {
                    string xmltext = propertyValue.Value == null ? string.Empty : ((propertyValue.Value as InArgument<XElement>).Expression as VisualBasicValue<XElement>).ExpressionText;

                    XmlForm dialog = new XmlForm(xmltext);
                    dialog.ShowDialog();
                    if (dialog.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        if (string.IsNullOrEmpty(dialog.XmlText))
                            propertyValue.Value = null;
                        else
                        {
                            var vbv = new VisualBasicValue<XElement>(dialog.XmlText);
                            propertyValue.Value = new InArgument<XElement>(vbv);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                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