示例#1
0
        void CreateCodeEditor()
        {
            var editorSvc = Services.GetComponentService <IVsEditorAdaptersFactoryService>();

            codeWindow = editorSvc.CreateVsCodeWindowAdapter(Services.ServiceProvider);
            // disable splitter since it will cause a crash
            var codeWindowEx = (IVsCodeWindowEx)codeWindow;
            var initView     = new INITVIEW[1];

            ErrorHandler.ThrowOnFailure(codeWindowEx.Initialize(
                                            (uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER,
                                            VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter,
                                            szNameAuxUserContext: string.Empty,
                                            szValueAuxUserContext: string.Empty,
                                            InitViewFlags: 0,
                                            pInitView: initView));
            ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(textBuffer));

            //needed for xeto/jeto files, not implemented for c# etc so we don't worry about the result
            Guid clsIdView = VSConstants.LOGVIEWID.TextView_guid;

            //codeWindow.SetViewClassID(ref clsIdView);

            if (ErrorHandler.Succeeded(codeWindow.GetPrimaryView(out viewAdapter)))
            {
                // get the view first so host is created
                //var wpfView = editorSvc.GetWpfTextView(viewAdapter);

                wpfViewHost = editorSvc.GetWpfTextViewHost(viewAdapter);

                System.Windows.FrameworkElement wpfElement = wpfViewHost?.HostControl;
                if (wpfElement != null)
                {
                    // get real host?
                    var parent = VisualTreeHelper.GetParent(wpfElement) as System.Windows.FrameworkElement;
                    while (parent != null)
                    {
                        wpfElement = parent;
                        parent     = VisualTreeHelper.GetParent(wpfElement) as System.Windows.FrameworkElement;
                    }

                    editorControl.Content = wpfElement.ToEto();
                    return;
                }
            }
            // something went wrong
            editorControl.Content = new Scrollable {
                Content = new Label {
                    Text = "Could not load editor"
                }
            };
        }