Excel VSTO Addin显示/隐藏任务窗格 [英] Excel VSTO Addin showing/hiding taskpane

查看:686
本文介绍了Excel VSTO Addin显示/隐藏任务窗格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做教程。一切都可以正常工作,一个空白的excel页面



https://msdn.microsoft.com/en-us/library/bb608590(v = vs.120).aspx



当我加载一个excel表,有人给了我,并去点击toggleButton1显示窗格我得到



{任务窗格已被删除或者否则不再有效。}



在线

  private void toggleButton1_Click(object sender,RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible =((RibbonToggleButton)sender).Checked;
}

指向该任务窗格的指针不知何故将消失?

  Microsoft.Office.Tools.CustomTaskPane PartPhotoTaskPane; 
Globals.ThisAddIn.Application.WindowActivate + = Application_WindowActivate;

void Application_WindowActivate(Excel.Workbook Wb,Excel.Window Wn)
{
if(PartPhotoTaskPane!= null)
{
PartPhotoTaskPane.Dispose );
InitalizePartPhotoViewerTaskPane(EPPF);
}
else
{
InitalizePartPhotoViewerTaskPane(EPPF);
}
}

///< summary>
///启动零件照片查看器任务窗格
///< / summary>
private void InitalizePartPhotoViewerTaskPane(ExcelPartPhotoFunction _EPPF)
{
//初始化零件搜索
try
{
PartPhotoTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add新的PartPhotoSearchPane(_EPPF),Part Information,Globals.ThisAddIn.Application.ActiveWindow);
PartPhotoTaskPane.Visible = Properties.Settings.Default.InfoPaneOpenStatus;
PartPhotoTaskPane.Width = 260;
}
catch(异常e)
{
MessageBox.Show(启动零件信息工具栏时出错)+ Environment.NewLine +
e.Message + Environment。 NewLine + e.StackTrace,Error!,MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

ATTEMPT 1:



点击事件仍然出错。我猜这是与我需要在课堂之间分享这个窗格的东西?

  private void toggleButton1_Click(object sender,RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible =((RibbonToggleButton)sender).Checked;
}


private void ExcelEvents_WorkbookActivate(Excel.Workbook wb)
{
var taskPane =(customTaskPanes.Where(kvp => kvp.Key .Target == wb).Select(kvp => kvp.Value).FirstOrDefault());

if(taskPane == null)
{
UcCenlarInvest control = new UcCenlarInvest();
taskPane = this.CustomTaskPanes.Add(控件,我的工作簿窗格+ wb.Name);
customTaskPanes [new WeakReference(wb)] = taskPane;
}
}

ATTEMPT 2:



所以现在Ribbon类可以得到TaskPane但仍然得到相同的错误。添加:

  private CustomTaskPane taskPane; 
public CustomTaskPane TaskPane
{
get
{
// return(customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select kvp => kvp.Value).FirstOrDefault());
返回窗格;
}
set
{
taskPane = value;
}
}

.....

  TaskPane =(customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value)。 FirstOrDefault()); 


解决方案

Excel 2016是一个单一的文档界面(SDI)单个Excel实例中的每个工作簿都包含自己的功能区UI。 a>



如果您打开新的工作簿,将显示一个新的功能区,但是指向任务窗格的指针将丢失。如果没有任务窗格已经存在,您应该实现一个WorkbookActivate事件并添加一个新的任务窗格。您还应该保留指向自定义任务窗格的静态列表。



请参阅此解决方案: Excel中的CustomTaskPane不会显示在新的工作簿中


I do the tutorial here. Everything works fine with a blank excel page

https://msdn.microsoft.com/en-us/library/bb608590(v=vs.120).aspx

When I load up a excel sheet someone gave me and go to click the toggleButton1 to show the pane I get

{"The taskpane has been deleted or is otherwise no longer valid."}

on the line

   private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
    {
        Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
    }

Is a pointer to that task pane somehow going away?

Microsoft.Office.Tools.CustomTaskPane PartPhotoTaskPane;
Globals.ThisAddIn.Application.WindowActivate += Application_WindowActivate;

        void Application_WindowActivate(Excel.Workbook Wb, Excel.Window Wn)
    {
        if (PartPhotoTaskPane != null)
        {
            PartPhotoTaskPane.Dispose();
            InitalizePartPhotoViewerTaskPane(EPPF);
        }
        else
        {
            InitalizePartPhotoViewerTaskPane(EPPF);
        }
    }

    /// <summary>
    /// Start up the part photo viewer task pane
    /// </summary>
    private void InitalizePartPhotoViewerTaskPane(ExcelPartPhotoFunctions _EPPF)
    {
        //intialize the part search
        try
        {
            PartPhotoTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new PartPhotoSearchPane(_EPPF), "Part Information", Globals.ThisAddIn.Application.ActiveWindow);
            PartPhotoTaskPane.Visible = Properties.Settings.Default.InfoPaneOpenStatus;
            PartPhotoTaskPane.Width = 260;
        }
        catch (Exception e)
        {
            MessageBox.Show("Error starting Part Info Toolbar:" + Environment.NewLine +
            e.Message + Environment.NewLine + e.StackTrace, "Error!", MessageBoxButtons.OK,
            MessageBoxIcon.Error);
        }
    }

ATTEMPT 1:

Still error on the click event. I am guessing it is something related to me needing to share that pane between classes?

private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
    {
        Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
    }


  private void ExcelEvents_WorkbookActivate(Excel.Workbook wb)
    {
        var taskPane = (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());

        if (taskPane == null)
        {
            UcCenlarInvest control = new UcCenlarInvest();
            taskPane = this.CustomTaskPanes.Add(control, "My pane for workbook " + wb.Name);
            customTaskPanes[new WeakReference(wb)] = taskPane;
        }
    }

ATTEMPT 2:

So now the Ribbon class can get the TaskPane yet I Still get the same error. Added this :

  private CustomTaskPane taskPane;
    public CustomTaskPane TaskPane
    {
        get
        {
            //return (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());
              return pane;
        }
        set
        {
            taskPane = value;
        }
    }

.....

  TaskPane = (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());

解决方案

Excel 2016 is a single document interface (SDI), each workbook in a single instance of Excel contains its own ribbon UI. more information

If you open a new workbook, a new ribbon appears, however the pointer to the taskpane is lost. You should implement a WorkbookActivate event and add a new task pane if no task pane already exist for it. You should also keep a static list of pointer to the custom taskpanes.

see this solution : CustomTaskPane in Excel doesn't appear in new Workbooks

这篇关于Excel VSTO Addin显示/隐藏任务窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆