كيف يمكنك إلغاء عملية إغلاق الأدوات أو Visual Studio IDE عبر vspackage؟

StackOverflow https://stackoverflow.com/questions/2385146

سؤال

انا املك 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;
    }

ال OnClose و WindowClosing طرق تطلق النار عند توقعها ، لكنني لا أجد طريقة لإلغاء الإغلاق. ماذا ينقصني؟ هل هناك أحداث مختلفة مطلوبة لإلغاء إغلاق؟

هل كانت مفيدة؟

المحلول

للاكتمال ، هناك طريقة مفيدة لإلغاء إغلاق مع نافذة الأداة (المقدمة من المستخدم Chicobo في منتدى MSDN وتكرار هنا).

  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;
    }
    

نصائح أخرى

لغرض إكمال الإجابة السابقة ، يستهدف مقتطف الكود التالي جزءًا لم تتم الإجابة عليه من السؤال وهو ما يدور حول كيفية منع تشغيل Visual Studio IDE داخل VsPackage:

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

استخدم الرمز أعلاه داخل فئة الحزمة الخاصة بك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top