Getting Started

The most important part of VSTO Contrib I believe is the RibbonFactory, it gives us a hook to use IoC, DI and a better development model with VSTO. This requires hooking into the VSTO startup at the right time, currently this is done by hooking into the CreateRibbonExtensibilityObject method, and the InternalStartup method of ThisAddin. Here is a sample Outlook add-in.

    public partial class ThisAddIn
    {
        private AddinBootstrapper _core;

        private static void ThisAddInStartup(object sender, EventArgs e)
        {
            if (System.Windows.Application.Current == null)
                new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };

            //Check for updates
            new VstoClickOnceUpdater()
                .CheckForUpdateAsync(
                    r =>
                    {
                        if (r.Updated)
                        {
                            MessageBox.Show(@"Twitter feed add-in updated");
                        }
                    });
        }

        protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new OutlookRibbonFactory(typeof(AddinBootstrapper).Assembly);
        }

        private void ThisAddInShutdown(object sender, EventArgs e)
        {
            _core.Dispose();
            System.Windows.Application.Current.Shutdown();
        }

        private void InternalStartup()
        {
            _core = new AddinBootstrapper();
            OutlookRibbonFactory.SetApplication(Application);
            RibbonFactory.Current.InitialiseFactory(
                t => (IRibbonViewModel)_core.Resolve(t),
                CustomTaskPanes);

            Startup += ThisAddInStartup;
            Shutdown += ThisAddInShutdown;
        }
    }


Then create a MyAddinCore project, you don't have to put Core on the end, the main thing is your create another class library and reference it from your add-in. Then create a bootstrapper class. Here is a simple which uses Autofac as the IoC container. In the future there will be increased support for releasing components, but for the moment, this will do.

    public class AddinBootstrapper : IDisposable
    {
        private readonly IContainer _container;

        public AddinBootstrapper()
        {
            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterType<TwitterService>().As<ITwitterService>();
            containerBuilder.RegisterType<ContactFeed>()
                .As<IRibbonViewModel>()
                .AsSelf();
            _container = containerBuilder.Build();
        }

        public object Resolve(Type type)
        {
            return _container.Resolve(type);
        }

        public T Resolve<T>()
        {
            return _container.Resolve<T>();
        }

        public void Dispose()
        {
            _container.Dispose();
        }
    }


After that you can create viewmodels and ribbon xml in your class library, download the source code and have a look at the demo projects for the moment until I create the create a new ViewModel wiki page

For more information have a look at this blog post

Last edited May 30, 2011 at 10:41 AM by JakeGinnivan, version 2

Comments

No comments yet.