Last active
April 10, 2020 10:39
-
-
Save tmatz/3f6ee339216bf336357ab5256ef53586 to your computer and use it in GitHub Desktop.
Show InfoBar: visual studio extension sample described at https://www.visualstudiogeeks.com/extensions/how-to-show-notifications-inside-visual-studio-2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EnvDTE; | |
using Microsoft.VisualStudio.Imaging; | |
using Microsoft.VisualStudio.Shell; | |
using Microsoft.VisualStudio.Shell.Interop; | |
using System; | |
using System.Collections.Generic; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
using Task = System.Threading.Tasks.Task; | |
namespace VSIXInfoBarProject | |
{ | |
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)] | |
[Guid(VSIXInfoBarProjectPackage.PackageGuidString)] | |
[ProvideAutoLoad(UIContextGuids80.SolutionHasSingleProject, PackageAutoLoadFlags.BackgroundLoad)] | |
[ProvideAutoLoad(UIContextGuids80.SolutionHasMultipleProjects, PackageAutoLoadFlags.BackgroundLoad)] | |
public sealed class VSIXInfoBarProjectPackage : AsyncPackage, IVsInfoBarUIEvents | |
{ | |
private class FileOpenContext | |
{ | |
public string FilePath { get; set; } | |
} | |
public const string PackageGuidString = "27482cd9-ac27-4bb8-ac49-46b72c00ddfb"; | |
private DTE _dte; | |
private IVsShell _shell; | |
private IVsInfoBarUIFactory _infoBarFactory; | |
private IVsInfoBarHost _infoBarHost; | |
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress) | |
{ | |
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); | |
_dte = GetGlobalService(typeof(DTE)) as DTE; | |
_shell = GetGlobalService(typeof(SVsShell)) as IVsShell; | |
_infoBarFactory = GetGlobalService(typeof(SVsInfoBarUIFactory)) as IVsInfoBarUIFactory; | |
if (_shell != null) | |
{ | |
_shell.GetProperty((int)__VSSPROPID7.VSSPROPID_MainWindowInfoBarHost, out var obj); | |
_infoBarHost = obj as IVsInfoBarHost; | |
} | |
var element = ShowInfoBar("title", "message"); | |
} | |
public IVsInfoBarUIElement ShowInfoBar(string title, string message, string path = null) | |
{ | |
ThreadHelper.ThrowIfNotOnUIThread(); | |
if (_dte == null || _shell == null || _infoBarHost == null || _infoBarFactory == null) | |
{ | |
return null; | |
} | |
var textSpan = new InfoBarTextSpan($"{title}: {message}"); | |
var actions = new List<IVsInfoBarActionItem> | |
{ | |
new InfoBarHyperlink("Yes", "yes"), | |
new InfoBarHyperlink("No", "no") | |
}; | |
if (path != null) | |
{ | |
actions.Add(new InfoBarHyperlink("Open", new FileOpenContext { FilePath = path })); | |
} | |
var model = new InfoBarModel(new[] { textSpan }, actions, KnownMonikers.StatusInformation, isCloseButtonVisible: true); | |
IVsInfoBarUIElement element = _infoBarFactory.CreateInfoBar(model); | |
element.Advise(this, out var _ /* cookie */); | |
_infoBarHost.AddInfoBar(element); | |
return element; | |
} | |
void IVsInfoBarUIEvents.OnClosed(IVsInfoBarUIElement infoBarUIElement) | |
{ | |
} | |
void IVsInfoBarUIEvents.OnActionItemClicked(IVsInfoBarUIElement infoBarUIElement, IVsInfoBarActionItem actionItem) | |
{ | |
ThreadHelper.ThrowIfNotOnUIThread(); | |
if (actionItem.ActionContext is FileOpenContext context) | |
{ | |
if (System.IO.File.Exists(context.FilePath)) | |
{ | |
_dte?.ItemOperations.OpenFile(context.FilePath); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment