Example #1
0
        private PackageManagerControl GetProjectPackageManagerControl(string projectUniqueName)
        {
            return(NuGetUIThreadHelper.JoinableTaskFactory.Run(async() =>
            {
                await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                var uiShell = ServiceLocator.GetGlobalService <SVsUIShell, IVsUIShell>();
                foreach (var windowFrame in VsUtility.GetDocumentWindows(uiShell))
                {
                    object docView;
                    var hr = windowFrame.GetProperty(
                        (int)__VSFPROPID.VSFPROPID_DocView,
                        out docView);
                    if (hr == VSConstants.S_OK &&
                        docView is PackageManagerWindowPane)
                    {
                        var packageManagerWindowPane = (PackageManagerWindowPane)docView;
                        if (packageManagerWindowPane.Model.IsSolution)
                        {
                            // the window is the solution package manager
                            continue;
                        }

                        var projects = packageManagerWindowPane.Model.Context.Projects;
                        if (projects.Count() != 1)
                        {
                            continue;
                        }

                        IProjectContextInfo existingProject = projects.First();
                        IServiceBroker serviceBroker = packageManagerWindowPane.Model.Context.ServiceBroker;
                        IProjectMetadataContextInfo projectMetadata = await existingProject.GetMetadataAsync(
                            serviceBroker,
                            CancellationToken.None);

                        if (string.Equals(projectMetadata.Name, projectUniqueName, StringComparison.OrdinalIgnoreCase))
                        {
                            var packageManagerControl = VsUtility.GetPackageManagerControl(windowFrame);
                            if (packageManagerControl != null)
                            {
                                return packageManagerControl;
                            }
                        }
                    }
                }

                return null;
            }));
        }
        private async Task <IVsWindowFrame> FindExistingSolutionWindowFrameAsync()
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var uiShell = await _asyncServiceProvider.GetServiceAsync <SVsUIShell, IVsUIShell>();

            foreach (var windowFrame in VsUtility.GetDocumentWindows(uiShell))
            {
                object property;
                var    hr = windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out property);
                var    packageManagerControl = VsUtility.GetPackageManagerControl(windowFrame);
                if (hr == VSConstants.S_OK &&
                    property is IVsSolution &&
                    packageManagerControl != null)
                {
                    return(windowFrame);
                }
            }

            return(null);
        }