locked
How can the first TextDocumentCreated event be caught via MEF? RRS feed

  • Question

  • Hi,

    My MEF component subscribes for the ITextDocumentFactoryService.TextDocumentCreated event via MEF import, and uses this event to manipulate things in the text document of certain file types. This component does its job well.

    MEF sets the ITextDocumentFactoryService import when the first text document loaded into the environment. This is the first occasion to add my TextDocumentCreated event handler. My problem is that my event handler will never be called for the first text document opened.

    If I open additional text documents, my event handler is regurarly called by the environment. I think it's a MEF problem, but until now I didn't find any workaround for this problem.

    Any help would be appreciated.

    Thanks,
    Norbert


    Wednesday, September 14, 2011 4:29 PM

Answers

  • I found a better method to achieve my purpose. MEF and ITextDocumentFactoryService is not suitable for it  because of the things above.


    The use of IVsRunningDocumentTable and IVsRunningDocTableEvents interfaces instead of MEF imports solved my problem. I could subscribe for RDT events in due time when my custom project package is constructed  and initialized by the shell. Thus, my component doesn't miss any important document event.

    The only flaw of this solution against MEF the requirement of a custom VS package.

    • Marked as answer by norbert.laszlo Thursday, September 15, 2011 10:22 PM
    Thursday, September 15, 2011 10:21 PM

All replies

  • Hi Norbert,

    It seems your event handler works in general but not for first text document opened. Where and when you sink the event handler for TextDocumentCreated?

    Yi 


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 15, 2011 7:49 AM
  • Hi Yi,

    Thank you for your response.

    There is another scenario where my event handler is not called. During the solution load phase when one or more text document is initially loaded by the solution.

    This is my code snippet:

    [Export(typeof(IClassifierProvider))]
    [ContentType("text")]
    internal class EditorClassifierProvider : IClassifierProvider
    {
       ...
       ITextDocumentFactoryService textDocumentFactory = null;
       [Import]
       internal ITextDocumentFactoryService TextDocumentFactory
       {
           set
           {
               // Text document factory service, set via MEF
               textDocumentFactory = value;
               textDocumentFactory.TextDocumentCreated += new EventHandler<TextDocumentEventArgs>(tdfs_TextDocumentCreated);
           }
       }
    
       void tdfs_TextDocumentCreated(object sender, TextDocumentEventArgs e)
       {
           // I should manipulate the content of the text document here, but
           // this event handler is not called for documents that are initially
           // loaded by the solution or for the first document opened by the user.
       }
    }
    

     

    Can I acquire the ITextDocumentFactoryService by another way than via MEF? MSDN says that this is a MEF component part.

    I have a custom project system with specific source files. These source files contain not only the program source code, but also contain additional meta information associated to the source. This meta information must be removed (hide) from each document in this event handler before the source is displayed in the language service. Later I replace the removed parts in the document (using the ITextDocument.FileActionOccurred event) when the document is saved.

    Unfortunately, my source manipulation algorithm is not called in the mentioned cases, because my event handler is not called. It seems the ITextDocumentFactoryService import is set too late and my component misses important events occurred in initial load phases. But, in later time, everything is ok.

    Thanks,
    Norbert



    Thursday, September 15, 2011 9:00 AM
  • I found a better method to achieve my purpose. MEF and ITextDocumentFactoryService is not suitable for it  because of the things above.


    The use of IVsRunningDocumentTable and IVsRunningDocTableEvents interfaces instead of MEF imports solved my problem. I could subscribe for RDT events in due time when my custom project package is constructed  and initialized by the shell. Thus, my component doesn't miss any important document event.

    The only flaw of this solution against MEF the requirement of a custom VS package.

    • Marked as answer by norbert.laszlo Thursday, September 15, 2011 10:22 PM
    Thursday, September 15, 2011 10:21 PM