c# - 如何通过 VSPackage 取消 ToolWindowPane 或 Visual Studio IDE 关闭操作?

标签 c# visual-studio vs-extensibility vspackage

我有一个 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
    }

还有来自实现 IVsWindowFrameNotify3ToolWindowPane 的一些简介:

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

OnCloseWindowClosing 方法会按预期触发,但我找不到取消关闭的方法。我错过了什么?取消关闭需要不同的事件吗?

最佳答案

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

  1. 在您的 ToolWindowPane 类中,实现 IVsWindowFrameNotify2 接口(interface),该接口(interface)提供方法 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;
    }
    

关于c# - 如何通过 VSPackage 取消 ToolWindowPane 或 Visual Studio IDE 关闭操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2385146/

相关文章:

c# - 从 SQL 加载数据以组合并保存所选值

visual-studio - 为 win8 应用程序和 Visual Studio 2013 创建带有框架目标的 Nuget 包

visual-studio - Visual Studio Post Build 不喜欢我的条件

c++ - 修复库代码出现编译错误的方法

c# - DTE2 事件不触发

c# - 如何在不编译项目的情况下获取类的元数据

c# - 在未能使用网络级身份验证向远程桌面服务器提供正确凭据后防止登录尝试失败窗口

c# - 不可调用成员不能像方法一样使用

wpf - VS 2012 扩展中的标准控件

c# - 如何使用 NServiceBus 执行集成测试?