none
NewInspector and Close event does not raise if wpf control used in task pane RRS feed

  • Question

  • I have problem with WPF control inside task pane. I added it using ElementHost. So, if I show task pane, then NewInspector and inspector Close doesn't raise anymore.
    Here is my code
        
        
    <?xml version="1.0" encoding="UTF-8"?>
        <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
          <ribbon>
            <tabs>
              <tab idMso="TabNewMailMessage">        
                <group label="MyGroup" id="mygroup">
                  <button id="addItemBlabla"
                          size="large"                  
                          label="Add Items"
                          onAction="AddItemsClick"/>
                </group>        
              </tab>
            </tabs>
         </ribbon>
        </customUI>
         
         //..Ribbon.cs file
     
         public void AddItemsClick(Office.IRibbonControl ribbonControl)
         {
            Inspector inspector = ribbonControl.Context as Inspector;
            if (this.taskPane == null)
            {
                this.taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new MyUserControlThatContainsWpfElementHost(), "Label", this.inspector);
            }
    
            this.taskPane.Visible = true;     
         }
    
         //.. ThisAddIn.cs file
    
          private void ThisAddIn_Startup(object sender, System.EventArgs e)
          {
               this.Application.Inspectors.NewInspector += this.Inspectors_NewInspector;            
          }
    
          private void Inspectors_NewInspector(Outlook.Inspector Inspector)
          {   
              //Does not work after task pane is shown!!!         
          }
    This example works if I'm using Ribbon in designer, but I need to use XML version



    Monday, June 13, 2016 1:28 PM

Answers

  • >>>I have problem with WPF control inside task pane. I added it using ElementHost. So, if I show task pane, then NewInspector and inspector Close doesn't raise anymore.

    According to your description, I suggest that you could modify your code like below:
    private Microsoft.Office.Interop.Outlook.Inspectors objInspectors;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
       objInspectors = this.Application.Inspectors;
       objInspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(this.Inspectors_NewInspector);
    }

    Tuesday, June 14, 2016 7:59 AM

All replies

  • >>>I have problem with WPF control inside task pane. I added it using ElementHost. So, if I show task pane, then NewInspector and inspector Close doesn't raise anymore.

    According to your description, I suggest that you could modify your code like below:
    private Microsoft.Office.Interop.Outlook.Inspectors objInspectors;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
       objInspectors = this.Application.Inspectors;
       objInspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(this.Inspectors_NewInspector);
    }

    Tuesday, June 14, 2016 7:59 AM
  • Wow, this works!

    Can you explain why is it fixes my problem. Because it looks like kind of a magic to me.


    Tuesday, June 14, 2016 8:47 AM
  • this.Application.Inspectors.NewInspector

    The Inspectors property of the Application class returns an instance of the corresponding class which is going to be swiped from the heap by the GC as soon as the Startup method finishes. So, GC can swipe it from the heap and you will never get any events handled. 

    To prevent it from happening you need to be sure the source object is always alive. For that you need to declare it at the add-in class level.


    [custom.development]

    Tuesday, June 14, 2016 12:45 PM