styx
styx

Reputation: 1925

IVsWindowFrame CloseFrame not working as intended on .xaml files

I'm trying to build a Visual Studio 2022 extension that closes tabs based on certain conditions.

When I'm using VsWindowFrame.CloseFrame() on most file types, it works as intended (the tab just closes).

But when I'm calling the above function on .xaml files, the content of the file disappears, but the tab is still open.

After I close the .xaml file from my code, when I press "Close" or on the "X" button, the tab is still not closing, but "Close All Tabs" or "Close Document Tabs" commands do work.

this i how call close window

  foreach (IVsWindowFrame frame in frames)
  {
      var result =   frame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_PromptSave);    
      //just for tests      
      var closedResult = result == VSConstants.S_OK; //true
      frame.IsOnScreen(out int isOnScreen);
      var IsOnScreenResult = isOnScreen == VSConstants.S_OK; //true
      var isVisibleResult = frame.IsVisible() == VSConstants.S_OK; //false
      var hideResult = frame.Hide() == VSConstants.S_OK; //false 

  }

I get the same error code -2146232798 (could not find what it means via VSConstants)

Upvotes: 0

Views: 65

Answers (1)

styx
styx

Reputation: 1925

I Manged to solve it by calling DTE2.ActiveDocument.Close()

foreach (IVsWindowFrame frame in frames)
{
    var CloseFrameResult =   frame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_PromptSave);
    frame.IsOnScreen(out int isOnScreen);  
    if(CloseFrameResult !=  VSConstants.S_OK || frame.IsVisilbe !=  VSConstants.S_OK || isOnScreen !=  VSConstants.S_OK) 
    {
       var dte = await ServiceProivder.GetGlobalServiceAsync(typeof(DTE)) as DTE2;
       if(dte?.ActiveDocumnet is { ActiveDocumnet : not null}
       {
           dte.ActiveDocumnet.Close()
       }
    }  

Upvotes: 0

Related Questions