Adding a Startup Command

3/30/2010 8:56 PM
You can subscribe to this wiki article using an RSS feed reader.

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.

Tags:
Home: SoapBox Core What's new: Recently changed articles