Open a WPF window app form VS extension command - c#

I'm writing my first VS extension.
so far i have the code to get selected text and display a message box or manipulate the selection:
the CTOR of the extention..
private StringRefactor(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
this.package = package;
OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
}
}
The callback:
private void MenuItemCallback(object sender, EventArgs e)
{
var selection = getSelection();
var selectedText = selection == null ? "No text selected..." : selection.StreamSelectionSpan.GetText();
string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
string title = "StringRefactor";
// Show a message box to prove we were here
VsShellUtilities.ShowMessageBox(
this.ServiceProvider,
selectedText,
title,
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
now instead of VsShellUtilities.ShowMessageBox(... i would like to open a prompt window that display several textboxes and ok\cancel button..
I thought of creating another WPF app project and launching it from the callback but I'm not sure this is the right way to write an extension that opens a custom tool ..
so what is the right way to open a custom window with functionality from a VISIX ?

You can create your own WPF dialogs in the VSIX extension. In fact Visual Studio is designed for that (since the UI is WPF).
See this article for further instructions:
Creating and Managing Modal Dialog Boxes

Related

Tracking changes in C# interactive window

VS now comes with an interactive window, but unlike running the raw CSI.EXE Roslyn process, Visual Studio adds IntelliSense and a few other features such as being able to load in the current project.
I want to write a VS plug-in that tracks all text editor changes in this window. Is this possible? What I'm looking for is something akin to PreviewKeyDown/PreviewTextInput WPF events. Can I get those on the C# interactive window and, if so, how?
Here's how far I got so far:
var dte = Shell.Instance.GetComponent<DTE>();
foreach (Window window in dte.MainWindow.Collection)
{
if (window.Kind.ToUpper().Contains("TOOL"))
{
if (window.Caption == "C# Interactive")
{
WpfWindow wpfWindow = (WpfWindow)HwndSource.FromHwnd((IntPtr) window.HWnd).RootVisual;
for (int i = 0; i < VTH.GetChildrenCount(wpfWindow); ++i)
{
// now what?
}
}
}
}
Here is some code that will get an IWpfTextViewHost reference on the C# interactive Window. From there, you can have access to all text services from Visual Studio: Text lines, Text buffer, etc. (or you can hook directly on WPF's controls, which I don't recommend)
// get global UI shell service from a service provider
var shell = (IVsUIShell)ServiceProvider.GetService(typeof(SVsUIShell));
// try to find the C# interactive Window frame from it's package Id
// with different Guids, it could also work for other interactive Windows (F#, VB, etc.)
var CSharpVsInteractiveWindowPackageId = new Guid("{ca8cc5c7-0231-406a-95cd-aa5ed6ac0190}");
// you can use a flag here to force open it
var flags = __VSFINDTOOLWIN.FTW_fFindFirst;
shell.FindToolWindow((uint)flags, ref CSharpVsInteractiveWindowPackageId, out IVsWindowFrame frame);
// available?
if (frame != null)
{
// get its view (it's a WindowPane)
frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out object dv);
// this pane implements IVsInteractiveWindow (you need to add the Microsoft.VisualStudio.VsInteractiveWindow nuget package)
var iw = (IVsInteractiveWindow)dv;
// now get the wpf view host
// using an extension method from Microsoft.VisualStudio.VsInteractiveWindowExtensions class
IWpfTextViewHost host = iw.InteractiveWindow.GetTextViewHost();
// you can get lines with this
var lines = host.TextView.TextViewLines;
// and subscribe to events in text with this
host.TextView.TextBuffer.Changed += TextBuffer_Changed;
}
private void TextBuffer_Changed(object sender, TextContentChangedEventArgs e)
{
// text has changed
}
Note "Microsoft.VisualStudio.VsInteractiveWindow" assembly is not specifically documented but the source is open: http://sourceroslyn.io/#Microsoft.VisualStudio.VsInteractiveWindow

How to open a tool window of a visual studio extension programmatically?

So I've got two tool windows in my visual studio extension (package) and I'd like to open up the second window via a button on the first window.
I expected this to be explained here: "How to: Open a Tool Window Programmatically", but it wasn't.
You should use either Package.FindToolWindow or IVsUIShell.FindToolWindow to find or create a tool window.
If used from your own package (or if you have a reference to the package, just put it there instead of this):
private void OpenFromPackage()
{
ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true); // True means: crate if not found. 0 means there is only 1 instance of this tool window
if (null == window || null == window.Frame)
throw new NotSupportedException("MyToolWindow not found");
IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
ErrorHandler.ThrowOnFailure(windowFrame.Show());
}
If you can't do it from your package, or don't have a reference to it, use IVSUIShell:
private void OpenWithIVsUIShell()
{
IVsUIShell vsUIShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));
Guid guid = typeof(MyToolWindow).GUID;
IVsWindowFrame windowFrame;
int result = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFindFirst, ref guid, out windowFrame); // Find MyToolWindow
if (result != VSConstants.S_OK)
result = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fForceCreate, ref guid, out windowFrame); // Crate MyToolWindow if not found
if (result == VSConstants.S_OK) // Show MyToolWindow
ErrorHandler.ThrowOnFailure(windowFrame.Show());
}
When you create a new package with toolwindow support, you get a single toolwindow and a command that displays it. This command is handled in the package class with the ShowToolWindow method.
Examining that, you'll see that the base package object has a FindToolWindow method that you can use to find (and create if needed) any toolwindow you have implemented in your package. That FindToolWindow method is just a nice wrapper around the IVsUIShell.FindToolWindow method, which is what ultimately gets invoked when displaying any toolwindow.
So instead of using the old EnvDTE automation interface, I would recommend using the lower level services built into the actual package object.
Here's how I solved it, the following code is the code-behind method of the button on the first window:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var dte = Package.GetGlobalService(typeof(DTE)) as DTE;
if (dte == null) return;
var window = dte.Windows.Item("{WindowGUID}");
window.Visible = true;
}
You should find the "WindowGUID" in the Guids class and above the class of the ToolWindow.

Change App language at RunTime on-the-fly

I'm currently developing a metro app in which the user can change current language at runtime and all the custom controls that are loaded must update their text regarding to the new language. Problem is that when I change the language using the following code, the app language changes but it will only update text when I restart my app because the pages and controls that are already rendered are cached.
LocalizationManager.UICulture = new System.Globalization.CultureInfo((string)((ComboBoxItem)e.AddedItems[0]).Tag);
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = ((ComboBoxItem)e.AddedItems[0]).Tag as String;
What should I do to force updating text of all custom controls at runtime without restarting my app?
Use this:
var NewLanguage = (string)((ComboBoxItem)e.AddedItems[0]).Tag;
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = NewLanguage;
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
//Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.Reset();
and then reload your Page, using Navigate method:
if (Frame != null)
Frame.Navigate(typeof(MyPage));
In order to respond right away, you would need to reset the context of the resource manager.
For Windows 8.1:
var resourceContext = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView();
resourceContext.Reset();
You will still need to force your page to redraw itself and thus re-request the resources to get the changes to take place. For Windows 8, you can see https://timheuer.com/blog/archive/2013/03/26/howto-refresh-languages-winrt-xaml-windows-store.aspx
You can change the app's language at runtime with the help of this source code. I took help from this and manipulated my app's language settings page as follows:
In languageSettings.xaml.cs:
public partial class LanguageSettings : PhoneApplicationPage
{
public LanguageSettings()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (ChangeLanguageCombo.Items.Count == 0)
{ ChangeLanguageCombo.Items.Add(LocalizationManager.SupportedLanguages.En);
ChangeLanguageCombo.Items.Add(LocalizationManager.SupportedLanguages.Bn);
}
SelectChoice();
}
private void ButtonSaveLang_OnClick(object sender, RoutedEventArgs e)
{
//Store the Messagebox result in result variable
MessageBoxResult result = MessageBox.Show("App language will be changed. Do you want to continue?", "Apply Changes", MessageBoxButton.OKCancel);
//check if user clicked on ok
if (result == MessageBoxResult.OK)
{
var languageComboBox = ChangeLanguageCombo.SelectedItem;
LocalizationManager.ChangeAppLanguage(languageComboBox.ToString());
//Application.Current.Terminate(); I am commenting out because I don't neede to restart my app anymore.
}
else
{
SelectChoice();
}
}
private void SelectChoice()
{
//Select the saved language
string lang = LocalizationManager.GetCurrentAppLang();
if(lang == "bn-BD")
ChangeLanguageCombo.SelectedItem = ChangeLanguageCombo.Items[1];
else
{
ChangeLanguageCombo.SelectedItem = ChangeLanguageCombo.Items[0];
}
}
}
***Note: Before understanding what I did on LanguageSettings page's code behind, you must implement the codes from the link as stated earlier. And also it may be noted that I am working on windows phone 8

Webdriver, CSharp, C# - Cannot switch between windows

I am stuck when trying to switch windows using the latest version of webdriver in C#.
I have a base window, when i click a button, it opens a new window.
The target code for this button is as below.
window.open(uri, "DisplayPage", " width=1200, scrollbars=yes , resizable = yes , toolbar = no , menubar = no");
I am using the below mentioned code to target the new window
string BaseWindow = _driver.CurrentWindowHandle;
ReadOnlyCollection<string> handles = _driver.WindowHandles;
foreach (string handle in handles)
{
if (handle != BaseWindow)
{
_driver.SwitchTo().Window(handle).Title.Equals("DisplayPage");
}
}
}
As you can see from above, I am switching to the window using the Target Title from the base window. This does not seem to work.
I then noticed that the Title of the opened window was different, it was "Display - Transaction Page"
I then modified the code to this
string BaseWindow = _driver.CurrentWindowHandle;
ReadOnlyCollection<string> handles = _driver.WindowHandles;
foreach (string handle in handles)
{
if (handle != BaseWindow)
{
_driver.SwitchTo().Window(handle).Title.Equals("Display - Transaction Page");
}
}
}
Still no luck.
Interestingly, I do not get any errors saying "Window not found".
The problem is that When i try to click on elements on the newly opened page, i get a NoSuchElementException which means that the newly opened window has not been targeted.
Any ideas?
Regards,
Hasan
You should break the loop once window changed to your window, otherwise it will always switch to last opened window:
foreach (string handle in handles) {
if (handle != BaseWindow) {
if(_driver.SwitchTo().Window(handle).Title.Equals("Display - Transaction Page"))
break;
}
}
You can try with Contains instead of equal, it will simplify the window search:
_driver.SwitchTo().Window(handle).Title.Contains("Display");
Although you cracked the answer yourself, there is another way to handle switching between the windows in C#.
// initiate webdriver
IWebDriver driver = new FirefoxDriver();
//perform some action to open a new window. Like clicking a link.
driver.FindElement(By.Id("btnId")).Click();
//switch to new window.
driver.SwitchTo().Window(driver.WindowHandles.Last());
//if you want to switch back to your first window
driver.SwitchTo().Window(driver.WindowHandles.First());

How to open new windows from main GUI and and pass vairable to the new window

I have an application with a Listbox with files and a menu. When I right-click an item from my listbox I have a menu for example Send. When I press 'Send' I want another window to open (I already have the new window) and in the new window I want to have the item-path that I selected (I have this path in the main window).
private void MenuItemSend_Click(object sender, RoutedEventArgs e)
{
if (listBoxFiles.SelectedIndex == -1)
{
return;
}
string filePath = (listBoxFiles.SelectedItem).ToString(); --- my file path
StatisticsWindow sForm = new StatisticsWindow();
sForm.ShowDialog(); -- open the new window
}
How can I do it ?
Thanks
Why don't you create a constructor for the window?
Instead of
new IpStatisticsWindow();
this:
new IpStatisticsWindow(filePath);
// In the IpStatisticsWindow class
public IpStatisticsWindow(string path)
{
//do something with path
}
You could of course also create a property or a method which handles it, then you can pass it there, e.g.
IPsForm.Path = filePath;
IPsForm.HandlePath(filePath);

Categories

Resources