Click here to Skip to main content
15,891,905 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.6K   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
//
//*****************************************************************************
//
#region namespaces
using System;
using System.Linq;
using System.Activities;
using System.Activities.Presentation;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.Converters;
using System.Activities.Presentation.PropertyEditing;
using System.Windows;
using System.Reflection;
using System.Xml.Linq;
using Microsoft.VisualBasic.Activities;
#endregion

namespace RKiss.MessageMediationActivityLibrary.Editors
{
    /// <summary>
    /// Editor for IDictionary<string, Argument> property
    /// </summary>
    public class DynamicArgumentDictionaryEditor : DialogPropertyValueEditor
    {
        public DynamicArgumentDictionaryEditor()
        {
            Type type = Type.GetType("System.Activities.Core.Presentation.Themes.EditorCategoryTemplateDictionary, System.Activities.Core.Presentation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
            Uri resourceLocator = new Uri(type.Assembly.GetName().Name + ";component/System/Activities/Core/Presentation/Themes/EditorCategoryTemplateDictionary.xaml", UriKind.RelativeOrAbsolute);
            ResourceDictionary resources = (ResourceDictionary)Application.LoadComponent(resourceLocator);
            base.InlineEditorTemplate = resources["ArgumentCollection_InlineTemplate"] as DataTemplate;
        }

        public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
        {
            ModelPropertyEntryToOwnerActivityConverter converter = new ModelPropertyEntryToOwnerActivityConverter();
            ModelItem activity = (ModelItem)converter.Convert(propertyValue.ParentProperty, typeof(ModelItem), false, null);
            ModelItem item2 = (ModelItem)converter.Convert(propertyValue.ParentProperty, typeof(ModelItem), true, null);

            //workaround: EditingContext context = ((IModelTreeItem)activity).ModelTreeManager.Context;
            Type type = Type.GetType("System.Activities.Presentation.Model.IModelTreeItem, System.Activities.Presentation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
            ModelTreeManager manager = (ModelTreeManager)type.GetProperties().FirstOrDefault(e => e.Name == "ModelTreeManager").GetValue(activity, null);
            EditingContext context = (EditingContext)typeof(ModelTreeManager).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(e => e.Name == "Context").GetValue(manager, null);

            ModelItemDictionary dictionary = item2.Properties[propertyValue.ParentProperty.PropertyName].Dictionary;
            DynamicArgumentDesignerOptions options = new DynamicArgumentDesignerOptions()
            {
                Title = propertyValue.ParentProperty.DisplayName
            };
            if (DynamicArgumentDialog.ShowDialog(activity, dictionary, context, activity.View, options))
            {

            }
        }

    }





    public class DynamicArgumentDictionaryEditor2 : DialogPropertyValueEditor
    {
        public DynamicArgumentDictionaryEditor2()
        {
            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/WorkflowModel/Design/View/EditorResources.xaml", UriKind.RelativeOrAbsolute);
            ResourceDictionary resources = (ResourceDictionary)Application.LoadComponent(resourceLocator);
            base.InlineEditorTemplate = resources["DynamicArgumentDictionaryInlineTemplate"] as DataTemplate;

            //System.Windows.Resources.StreamResourceInfo xamlResourceInfo = Application.GetResourceStream(resourceLocator);

            //using (System.IO.StreamReader sr1 = new System.IO.StreamReader(xamlResourceInfo.Stream))
            //{
            //    string xamstring = sr1.ReadToEnd();
            //    var xamlObject = System.Windows.Markup.XamlReader.LoadBaml(xamlResourceInfo.Stream, new System.Windows.Markup.ParserContext(), null, true);
            //}

        }
    }


    public class ExpressionValueEditor : DialogPropertyValueEditor
    {

        public ExpressionValueEditor()
        {
            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/WorkflowModel/Design/View/EditorResources.xaml", UriKind.RelativeOrAbsolute);
            ResourceDictionary resources = (ResourceDictionary)Application.LoadComponent(resourceLocator);
            base.InlineEditorTemplate = resources["inlineExpressionEditorTemplate"] as DataTemplate;

        }

        public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
        {
            string xmltext = ((propertyValue.Value as InArgument<XElement>).Expression as VisualBasicValue<XElement>).ExpressionText;
            MessageBox.Show(xmltext);

            (propertyValue.Value as InArgument<XElement>).Expression = new VisualBasicValue<XElement>("<abc>Hello world</abc>");
        }

    }
}

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