问题
SCENARIO
In Visual Studio 2017 I created a new extension (a VSIX project), then I added a new ToolWindow
item, which it is a WPF user control, and in the user control I added a TextBox
control.
This is the user control design:
And the relevant part of the generated XAML code:
<UserControl x:Class="ToolWindow1Control"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Name="MyToolWindow"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<StackPanel Orientation="Vertical">
<TextBox Text="..." />
</StackPanel>
</Grid>
</UserControl>
PROBLEM / QUESTION
The purpose of the ToolWindow
is to represent some text that will be generated at runtime with one of the commands defined in the extension package through an option in the context menu. In other words we could say that my intention is to create an Output Window for my extension, to show some kind of results in the TextBox...
To clarify it better, I know how to create an Output Pane that only can be shown in the list of the default Output Window of Visual Studio, here:
...I don't pretent to do that, what I pretend to do is an Output Window than can be docked here together with the other windows:
Then, I'm not totally sure but I think I'm on the right why choosing a ToolWindow to accomplish that task, and so I need to know how can I access the TextBox of the ToolWindow through other different location than the ToolWindow1.vb class. This is not a question about member visibility (Public, Private, etc), it is about the autogenerated ToolWindow1Control
object that represents my WPF user-control / ToolWindow, but I don't know the way to interact the TextBox through that object or other way to do it...
This is the relevant code of my VSIX extension to show at which step/location I need to access the control to set its text:
<PackageRegistration(UseManagedResourcesOnly:=True)>
<InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400)>
<ProvideMenuResource("Menus.ctmenu", 1)>
<ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)>
<Guid(Guids.PackageString)>
Public NotInheritable Class Main : Inherits Shell.Package
Private WithEvents MyCommand As OleMenuCommand
Protected Overrides Sub Initialize()
Me.InitializeMenuHandlers()
End Sub
Private Sub InitializeMenuHandlers()
Dim mcs As OleMenuCommandService = DirectCast(Me.GetService(GetType(IMenuCommandService)), OleMenuCommandService)
Dim MyCommandId As New CommandID( ... )
Me.MyCommand = New OleMenuCommand(AddressOf MyCommandCallback, MyCommandId)
mcs.AddCommand(MyCommand)
End Sub
Private Sub MyCommandCallback(sender As Object, e As EventArgs)
ErrorHandler.ThrowOnFailure(MyMethod(), {0})
End Sub
Friend Sub MyMethod()
' Here I need to interact with my ToolWindow controls (a TextBox) to change their text content...
End Sub
End Class
I'm not sure whether I'm focused on the right direction because I don't manage the WPF technology. I just created a ToolWindow and I need to set the text of a TextBox inside my ToolWindow from other part of the code different than the ToolWindow1.vb class. How can I do that?.
来源:https://stackoverflow.com/questions/47226505/how-do-i-interact-with-the-controls-of-a-toolwindow-in-a-visual-studio-extension