I want my add-in to execute some code when my application starts. How can I do that?

asked 31 May '10, 23:38

Scott%20Whitlock's gravatar image

Scott Whitlock ♦♦
696262833
accept rate: 50%


You add a startup command by extending the SoapBox.Core.ExtensionPoints.Host.StartupCommands extension point. The best way to illustrate is with an example. Here's a startup command that I use to set the application title in the workbench window (it also sets the icon in a really round-about way, by loading in a dummy form with the icon we want, and copying it from there):

namespace Your.Namespace.Here
{
    [Export(SoapBox.Core.ExtensionPoints.Host.StartupCommands,
                        typeof(IExecutableCommand))]
    class SetApplicationTitle : AbstractExtension, IExecutableCommand
    {
        public SetApplicationTitle()
        {
            ID = Extensions.Host.StartupCommands.SetApplicationTitle;
        }

        [Import(SoapBox.Core.CompositionPoints.Host.MainWindow)]
        private Lazy<Window> mainWindowExport { get; set; }

        public void Run(params object[] args)
        {
            Window mainWindow = mainWindowExport.Value;
            mainWindow.Title = Resources.Strings.Application_Title;

            // Sorry, this is a really hacky way of setting the icon on the
            // main window, and only because I can't seem to convert from
            // a PNG to an icon any other way.
            Dummy dummy = new Dummy();
            mainWindow.Icon = dummy.Icon;
            dummy.Close();
        }
    }
}

In case you're wondering where I'm setting my ID from, I created a file called Extensions.cs in my application's contracts project, and the structure looks like this:

namespace Your.Namespace.Here
{
    public static class Host
    {
        public static class StartupCommands
        {
            public const string SetApplicationTitle = "SetApplicationTitle";
        }
    }
}

That's a nice way to keep them organized, and if another extension comes along and wants to add a startup command that runs before or after this one, they can do it by referencing this constant.

link

answered 31 May '10, 23:41

Scott%20Whitlock's gravatar image

Scott Whitlock ♦♦
696262833
accept rate: 50%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×23
×1

Asked: 31 May '10, 23:38

Seen: 1,613 times

Last updated: 31 May '10, 23:41

powered by OSQA