2017-12-14 156 views
9

VS giờ đây có cửa sổ tương tác, nhưng không giống như chạy quy trình CSI.EXE thô, Visual Studio thêm IntelliSense và một vài tính năng khác như có thể tải trong dự án hiện tại.Theo dõi thay đổi trong cửa sổ tương tác C#

Tôi muốn viết một trình cắm thêm VS theo dõi tất cả thay đổi của trình soạn thảo văn bản trong cửa sổ này. Điều này có thể không? Những gì tôi đang tìm kiếm là một cái gì đó giống như PreviewKeyDown/PreviewTextInput sự kiện WPF. Tôi có thể lấy chúng trên cửa sổ tương tác C# và, nếu có, làm thế nào?

Sau đây là cách nay tôi có cho đến nay:

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? 
     } 
    } 
    } 
} 
+0

Meh, trường hợp khác * không có giấy tờ *, nghĩa là không có tài liệu để sử dụng' EnvDTE Interactive': https://docs.microsoft. com/en-us/dotnet/api/envdte.outputwindow.dte? view = visualstudiosdk-2017 –

+0

Bạn đang tìm kiếm IWpfTextViewHost của Cửa sổ tương tác C#? Điều đó có ổn không? –

+0

@SimonMourier không chắc chắn. Tôi cần một điều khiển cụ thể mà có thể 1) cho tôi toàn bộ bộ đệm của nó và 2) thông báo về bất kỳ thay đổi nào. về cơ bản tôi cần theo dõi mọi thay đổi trong cửa sổ tương tác. –

Trả lời

10

Dưới đây là một số mã mà sẽ nhận được một IWpfTextViewHost tham chiếu trên cửa sổ tương tác C#. Từ đó, bạn có thể có quyền truy cập vào tất cả các dịch vụ văn bản từ Visual Studio: dòng chữ, Text đệm, vv (hoặc bạn có thể treo trực tiếp trên điều khiển WPF, mà tôi không khuyên bạn nên)

// 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 không được ghi chép cụ thể nhưng nguồn mở: http://source.roslyn.io/#Microsoft.VisualStudio.VsInteractiveWindow

+0

Điều này có vẻ như nó có thể hoạt động! Câu hỏi nhanh: làm thế nào tôi sẽ đi về hooking vào kiểm soát VS cơ bản? Lý do tôi hỏi là tôi cũng thích thông tin lựa chọn và thông tin. –

+0

Mọi thứ sẽ có sẵn từ IWpfTextViewHost và bên dưới. Thú vị nhất là trong ITextView: Caret, Selection, vv https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.text.editor.itextview?view=visualstudiosdk-2017 –

+0

Nguồn là mở, câu trả lời hay! –

Các vấn đề liên quan