3

我有一个VSPackage包含表单数据的可停靠工具窗口。如果此表单中有未保存的更改,如果用户在关闭前单击取消保存更改,我想取消关闭工具窗口和 Visual Studio IDE。我可以在关闭时执行保存测试,但我没有看到任何事件处理程序方法或其他选项来实际取消关闭。

这是包装中的一些简介:

    private DTE2 _applicationObject = null;
    ///--------------------------------------------------------------------------------
    /// <summary>This property gets the visual studio IDE application object.</summary>
    ///--------------------------------------------------------------------------------
    public DTE2 ApplicationObject
    {
        get
        {
            if (_applicationObject == null)
            {
                // Get an instance of the currently running Visual Studio IDE
                DTE dte = (DTE)GetService(typeof(DTE));
                _applicationObject = dte as DTE2;
            }
            return _applicationObject;
        }
    }
    ///--------------------------------------------------------------------------------
    /// <summary>
    /// Initialization of the package; this method is called right after the package is sited, so this is the place
    /// where you can put all the initilaization code that rely on services provided by VisualStudio.
    /// </summary>
    ///--------------------------------------------------------------------------------
    protected override void Initialize()
    {
        Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
        base.Initialize();

        // add the event handlers
        if (ApplicationObject != null)
        {
            // wire up window events
            PackageWindowEvents = (WindowEvents)ApplicationObject.Events.get_WindowEvents(null);
            PackageWindowEvents.WindowClosing += new _dispWindowEvents_WindowClosingEventHandler(PackageWindowEvents_WindowClosing);

            // wire up solution events
            PackageSolutionEvents = ApplicationObject.Events.SolutionEvents;
            PackageSolutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(CheckOpenCurrentSolution);

            // wire up DTE events
            PackageDTEEvents = ApplicationObject.Events.DTEEvents;
            PackageDTEEvents.OnBeginShutdown += new _dispDTEEvents_OnBeginShutdownEventHandler(HandleVisualStudioShutdown);
        }
    }

    void PackageWindowEvents_WindowClosing(Window window)
    {
        // handle save/cancel scenarios
    }

还有一些来自ToolWindowPane实现的简介IVsWindowFrameNotify3

    protected override void OnClose()
    {
        base.OnClose();
    }
    public int OnClose(ref uint pgrfSaveOptions)
    {
        return (int)__FRAMECLOSE.FRAMECLOSE_PromptSave;
    }

OnCloseand方法在WindowClosing预期时触发,但我没有找到取消关闭的方法。我错过了什么?取消关闭是否需要不同的事件?

4

2 回答 2

2

为了完整起见,有一种有用的方法可以使用工具窗口取消关闭(由 msdn 论坛上的用户 Chicobo 提供,并在此处重复)。

  1. 在您的 ToolWindowPane 类中,实现 IVsWindowFrameNotify2 接口,该接口提供了一个方法OnClose

  2. 要取消关闭窗口,请考虑使用:

    public int OnClose(ref uint pgrfSaveOptions)
    {
        // Check if your content is dirty here, then
    
        // Prompt a dialog
        MessageBoxResult res = MessageBox.Show("This Document has been modified. Do you want to save the changes ?",
                      "Unsaved changes", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);
        // If the users wants to save
        if (res == MessageBoxResult.Yes)
        {
            // Handle with your "save method here"
        }
    
        if (res == MessageBoxResult.Cancel)
        {
            // If "cancel" is clicked, abort the close
            return VSConstants.E_ABORT;
        }
    
        // Else, exit
        return VSConstants.S_OK;
    }
    
于 2011-05-27T23:51:06.350 回答
2

为了完成上一个答案,以下代码片段针对问题的未回答部分,即关于如何防止 Visual Studio IDE 在 a 中的关闭操作VsPackage

    protected override int QueryClose(out bool pfCanClose)
    {
        pfCanClose = false;
        return VSConstants.S_OK;
    }

在你的包类中使用上面的代码。

于 2014-12-14T20:43:22.217 回答