Пример #1
0
        /// <summary>
        /// Adds the specified command filter implementation to the manager,
        /// with the specified explicit metadata.
        /// </summary>
        /// <param name="filter">The command filter instance, which does not need to
        /// be annotated with the <see cref="CommandFilterAttribute"/> attribute since
        /// it's provided explicitly.</param>
        /// <param name="metadata">Explicit metadata to use for the command filter,
        /// instead of reflecting the <see cref="CommandFilterAttribute"/>.</param>
        public void AddFilter(ICommandFilter filter, CommandFilterAttribute metadata)
        {
            Guard.NotNull(() => filter, filter);
            Guard.NotNull(() => metadata, metadata);

            var commandPackageGuid = new Guid(metadata.PackageId);
            var commandPackage = default(IVsPackage);
            vsShell.IsPackageLoaded(ref commandPackageGuid, out commandPackage);

            if (commandPackage == null)
                ErrorHandler.ThrowOnFailure(vsShell.LoadPackage(ref commandPackageGuid, out commandPackage));

            var serviceProvider = commandPackage as IServiceProvider;
            if (serviceProvider == null)
            {
                tracer.Error(Strings.CommandManager.CommandPackageNotServiceProvider(commandPackageGuid));
                return;
            }

            var mcs = serviceProvider.GetService<IMenuCommandService>();
            if (mcs == null)
            {
                tracer.Error(Strings.CommandManager.NoMenuCommandService(commandPackageGuid));
                return;
            }

            var groupId = new Guid(metadata.GroupId);
            var command = mcs.FindCommand(new CommandID(groupId, metadata.CommandId));
            if (command == null)
            {
                tracer.Error(Strings.CommandManager.CommandNotFound(commandPackageGuid, groupId, metadata.CommandId));
                return;
            }

            // \o/: for some reason this cast never works on VS2012, even with the proper assembly references :(.
            // So we resort to dynamic.
            // var command =  as OleMenuCommand;
            dynamic dynCommand = command.AsDynamicReflection();
            try
            {
                dynCommand.add_BeforeQueryStatus(new EventHandler((sender, args) =>
                {
                    try
                    {
                        filter.QueryStatus(new OleMenuCommandAdapter((OleMenuCommand)sender));
                    }
                    catch (Exception e)
                    {
                        tracer.Error(Strings.CommandManager.FilterFailed(filter, e));
                    }
                }));
            }
            catch (RuntimeBinderException)
            {
                // The command may not be an OleMenuCommand and therefore it wouldn't have the BeforeQueryStatus.
                tracer.Error(Strings.CommandManager.CommandNotOle(commandPackageGuid, metadata.GroupId, metadata.CommandId));
            }
        }
Пример #2
0
        /// <summary>
        /// Adds the specified command filter implementation to the manager,
        /// with the specified explicit metadata.
        /// </summary>
        /// <param name="filter">The command filter instance, which does not need to
        /// be annotated with the <see cref="CommandFilterAttribute"/> attribute since
        /// it's provided explicitly.</param>
        /// <param name="metadata">Explicit metadata to use for the command filter,
        /// instead of reflecting the <see cref="CommandFilterAttribute"/>.</param>
        public void AddFilter(ICommandFilter filter, CommandFilterAttribute metadata)
        {
            Guard.NotNull(() => filter, filter);
            Guard.NotNull(() => metadata, metadata);

            var commandPackageGuid = new Guid(metadata.PackageId);
            var commandPackage     = default(IVsPackage);

            vsShell.IsPackageLoaded(ref commandPackageGuid, out commandPackage);

            if (commandPackage == null)
            {
                ErrorHandler.ThrowOnFailure(vsShell.LoadPackage(ref commandPackageGuid, out commandPackage));
            }

            var serviceProvider = commandPackage as IServiceProvider;

            if (serviceProvider == null)
            {
                tracer.Error(Strings.CommandManager.CommandPackageNotServiceProvider(commandPackageGuid));
                return;
            }

            var mcs = serviceProvider.GetService <IMenuCommandService>();

            if (mcs == null)
            {
                tracer.Error(Strings.CommandManager.NoMenuCommandService(commandPackageGuid));
                return;
            }

            var groupId = new Guid(metadata.GroupId);
            var command = mcs.FindCommand(new CommandID(groupId, metadata.CommandId));

            if (command == null)
            {
                tracer.Error(Strings.CommandManager.CommandNotFound(commandPackageGuid, groupId, metadata.CommandId));
                return;
            }

            // \o/: for some reason this cast never works on VS2012, even with the proper assembly references :(.
            // So we resort to dynamic.
            // var command =  as OleMenuCommand;
            dynamic dynCommand = command.AsDynamicReflection();

            try
            {
                dynCommand.add_BeforeQueryStatus(new EventHandler((sender, args) =>
                {
                    try
                    {
                        filter.QueryStatus(new OleMenuCommandAdapter((OleMenuCommand)sender));
                    }
                    catch (Exception e)
                    {
                        tracer.Error(Strings.CommandManager.FilterFailed(filter, e));
                    }
                }));
            }
            catch (RuntimeBinderException)
            {
                // The command may not be an OleMenuCommand and therefore it wouldn't have the BeforeQueryStatus.
                tracer.Error(Strings.CommandManager.CommandNotOle(commandPackageGuid, metadata.GroupId, metadata.CommandId));
            }
        }