Reg. AddIn EventData

  • 08-05-2008 9:46 AM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 217

    Re: Reg. AddIn EventData

    Ramaraju,

    This depends on the version of VSTA you are using.

    >I am getting one error...The given key was not present in the dictionary
    This indicates that you are trying to pass a type that is not in your TypeInfrastructureManager (VSTA v 1) or your HostTypeMapProvider (VSTA v 2). 

    The issue here is that you are trying to pass a generic and ProxyGen does not support generics. 

    For VSTA v 1 (2005), there is a workaround.  Check out our EventSample or Tom Quinn's ShapeAppAdvancedCSharpGenerics sample for the v 1 workaround. 

    For VSTA v 2 there is no workaround at this time for generic event handling.  The SDK samples demonstrate how to use and pass custom event args using non-generic event handling.
    From ShapeAppCSharp (VSTA v 2):
    namespace Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp

    {

        using System;

     

        public delegate void AddedEventHandler(object sender, AddedEventArgs e);

        public class AddedEventArgs : EventArgs

        {

            private object addedItem;

     

            public AddedEventArgs(object addedItem)

            {

                this.addedItem = addedItem;

            }

     

            public object AddedItem

            {

                get { return this.addedItem; }

            }

        }

     

        public partial class Drawing

        {

            public event AddedEventHandler ShapeAdded;

     

            private void OnShapeAdded(object sender, AddedEventArgs e)

            {

                IShape shape = e.AddedItem as IShape;

     

                if (ShapeAdded != null)

                    ShapeAdded(this, new AddedEventArgs(shape));

            }

        }

    }

     




    From EventSample (VSTA v 1):

    V)  Generic Event Handling in VSTA

    Generic event handling is using the type System.EventHandler or System.EventHandler<T> to declare events instead of first declaring a System.Delegate then declaring an event based on that delegate.  For System.EventHandler events no additional steps are needed to use the events in VSTA.  For System.EventHandler<T> events, which use custom event args, three changes to the proxy file are necessary.  The first change in the proxy file is to the event declaration.  Here, a syntax error produced by ProxyGen must be corrected and global added to the type.  The event should be declared as below.

    Auto-generated definition:

    public event global::System.EventHandler`1[[Sample.CustomEventArgs, EventSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] CustomEvent

    Corrected definition with the type EventHandler<T>:

    public event global::System.EventHandler<global::Sample.CustomEventArgs> CustomEvent

    The second change to the proxy file is in the declaration of the custom event args.  Here the inheritance of System.EventArgs must be added.

    Auto-generated definition:

    public partial class CustomEventArgs : global::Microsoft.VisualStudio.Tools.Applications.IProxy

    Corrected definition with System.EventArgs inherited:

    public partial class CustomEventArgs : global::System.EventArgs, global::Microsoft.VisualStudio.Tools.Applications.IProxy

    The third change is the addition of the type System.EventHandler<CustomEventArgs> to the type infrastructure manager in both the proxy file and the host application.  While other types defined in the type infrastructure manager are mapped to specific instance of the type, this definition is not.  The purpose of this definition is to allow the type System.EventHandler<CustomEventArgs> to pass between the host application and the add-in, not to map it to a specific instance of this type.  Unlike the other type definition there is no “class name” or “key” to associate with this type; therefore, any unique non-empty string will do.  Without this definition, add-ins that hook to events of the type System.EventHandler<CustomEventArgs> will fail.

    Possible definition 1:

    Proxy Side:

    typeInfrastructureManager.CanonicalNameToTypeMap.Add("Sample, Sample.CustomEventHandler", typeof(DelegateProxy <global::System.EventHandler<global::EventSample.CustomEventArgs>>));

     

    typeInfrastructureManager.TypeToCanonicalNameMap.Add(typeof(global::System.EventHandler<global::Sample.CustomEventArgs>), "Sample, EventSample.CustomEventHandler");

     

    Host Side:

    hostType = typeof(global::System.EventHandler<global:: Sample.CustomEventArgs>);

    proxyType = typeof(DelegateProxy<global::System.EventHandler<global:: Sample.CustomEventArgs>>);

    typeInfrastructureManager.CanonicalNameToTypeMap.Add("Sample, Sample.CustomEventHandler", proxyType);

    typeInfrastructureManager.TypeToCanonicalNameMap.Add(hostType, "Sample, Sample.CustomEventHandler");

     

    Possible definition 2:

    Proxy Side:

    typeInfrastructureManager.CanonicalNameToTypeMap.Add("x", typeof(DelegateProxy<global::System.EventHandler<global::Sample.CustomEventArgs>>));

     

    typeInfrastructureManager.TypeToCanonicalNameMap.Add( typeof(global::System.EventHandler<global::Sample.CustomEventArgs>), "x");

    Host Side:

    hostType = typeof(global::System.EventHandler<global:: Sample.CustomEventArgs>);

    proxyType = typeof(DelegateProxy<global::System.EventHandler<global:: Sample.CustomEventArgs>>);

    typeInfrastructureManager.CanonicalNameToTypeMap.Add("x", proxyType);

    typeInfrastructureManager.TypeToCanonicalNameMap.Add(hostType, "x");


    I hope this helps.  If you have any other questions please let me know.

    Thanks!

    -Melody

Copyright Summit Software Company, 2008. All rights reserved.