Click here to Skip to main content
15,891,136 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.5K   2K   72  
This article describes a design, implementation, and usage of custom message mediation activities for a XAML workflow.
//*****************************************************************************
//    Description.....WF4.5 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.IO;
using System.Windows;
using System.Windows.Forms;
using ManageableServicesConsole.UserControls;
#endregion

namespace RKiss.WorkflowDesignerTester
{
    public partial class Form1 : Form
    {
        private string filepath;
        public DateTime? LoadedDT { get; set; }
        public string LoadedXaml { get; set; }

        public Form1()
        {
            InitializeComponent();
            this.LoadedDT = null;
        }

        public WorkflowDesignerUserControl Designer
        {
            get { return this.workflowDesignerUserControl1; }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SaveXamlFile();           
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.Designer.IsDirty)
            {
                var result = System.Windows.MessageBox.Show("Would you like to save all changes?", "Open new xaml", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                    this.SaveXamlFile();
                else if (result == MessageBoxResult.Cancel)
                    return;
            }
            var dialog = new OpenFileDialog()
            {
                Filter = "xaml|*.xaml|xamlx|*.xamlx|all|*.*",
                CheckFileExists = true,
                DefaultExt = ".xaml",
                Multiselect = false,
                RestoreDirectory = true,
                Title = "Open xaml file",
            };
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Filepath = dialog.FileName;
                this.LoadXamlFile();               
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LoadedDT = DateTime.Now;
            this.LoadedXaml = string.Empty;
            this.Designer.Xaml = string.Empty;
        }

        private void saveAsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            var dialog = new SaveFileDialog()
            {
                CheckPathExists = true,
                Filter = "xaml|*.xaml|xamlx|*.xamlx",
                AddExtension = true,
                OverwritePrompt = true,                
                CheckFileExists = false,
                DefaultExt = ".xaml",
                RestoreDirectory = true,
                Title = "Save xaml file",
            };
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if(string.IsNullOrEmpty(dialog.FileName) == false)
                    this.SaveAsXamlFile(dialog.FileName);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void LoadXamlFile()
        {
            this.LoadXamlFile(this.Filepath);
        }
        public void LoadXamlFile(string filename)
        {
            if (File.Exists(filename))
            {
                this.LoadedDT = DateTime.Now;
                this.LoadedXaml = File.ReadAllText(filename);
                this.Designer.Xaml = this.LoadedXaml;               
            }
        }

        public void SaveXamlFile()
        {
            this.SaveXamlFile(this.Filepath);
        }
        public void SaveXamlFile(string filename)
        {
            if (this.Designer.IsDirty)
            {
                var result = System.Windows.MessageBox.Show("Would you like to save all changes?", "Open new xaml", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                    File.WriteAllText(filename, this.Designer.Xaml);
                else if (result == MessageBoxResult.Cancel)
                    return;
            }
            this.Designer.IsDirty = false;
        }

        public void SaveAsXamlFile(string filename)
        {
            this.Filepath = filename;
            File.WriteAllText(filename, this.Designer.Xaml);
            this.Designer.IsDirty = false;
        }

        public string Filepath
        {
            get { return filepath; }
            set
            {
                filepath = value;
                this.Text = "Workflow Designer (" + value + ")";
            }
        }

        private void aboutWorkflowDesignerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new RKiss.WorkflowDesignerTester.Dialogs.WorkflowDesignerTesterAboutBox().ShowDialog();
        }
    }
}

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