System.InvalidCastException 同时将用户控件添加到 CustomTaskPane

问题描述 投票:0回答:2

我正在尝试使用以下行在

User control
上添加
Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane

myUserControl = new IssueAddition(categoryList);
myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl, "Issue Reporting Pane");  --> errornous line
myCustomTaskPane.Visible = true;

我收到以下异常:

 An exception of type 'System.InvalidCastException' occurred in Microsoft.Office.Tools.Common.Implementation.dll but was not handled in user code
Additional information: 
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.Tools.Office.Runtime.Interop.ICustomTaskPaneSite'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{3CA8CD11-274A-41B6-A999-28562DAB3AA2}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

我不确定

System.InvalidCastException
异常的原因是什么。

编辑 1:添加最新实现的代码也给出了同样的错误: 我有一个单独的类,

IssueAddition
继承
System.Windows.Forms.UserControl
类如下:

   public partial class IssueAddition : UserControl
    {
        public IssueAddition()
        {
            InitializeComponent();
        }

        public IssueAddition(List<String> categories)
        {
            InitializeComponent();
            foreach (string cat in categories)
                this.i_issue_category.Items.Add(cat);
        }
        public string IssueCategoryInputText
        {
            get { return (string)this.i_issue_category.SelectedItem; }
            set { this.i_issue_category.SelectedItem = 
            this.i_issue_category.FindStringExact(value); }
        }
        public string MessageInputText
        {
            get { return this.i_error_message.Text; }
            set { this.i_error_message.Text = value; }
        }
}

它被用于

ThisAddIn.cs
如下所示:

声明:

#region Instance Variables

        Outlook.Application m_Application;
        internal static List<Outlook.Explorer> m_Windows; // List of tracked explorer windows.
        internal static List<Outlook.Inspector> m_InspectorWindows;// List of traced inspector windows.
        private IssueAddition myUserControl; <--

用法:

public async void AddIssue()
        {
            List<String> categoryList = new List<String>();
            await Task.Run(() =>
            {
                categoryList = dAO.loadCategoryAsync("all").GetAwaiter().GetResult();
                if (categoryList == null)
                    categoryList = new List<String>();
                myUserControl = new IssueAddition(categoryList);
                Add add = new Add(MyCustomControlAdd);
                Globals.ThisAddIn.myUserControl.Invoke(add);                
            });
        }
        delegate void Add();

        private void MyCustomControlAdd()

        {
            Globals.ThisAddIn.CustomTaskPanes.Add(myUserControl, "test").Visible = true;
        }

Edit 2:我也试过

Invoke
方法但它也抛出同样的异常。我无法理解如何克服这个问题。

       public async void AddIssue()
        {
            List<String> categoryList = new List<String>();
            await Task.Run(() =>
            {
                categoryList = 
               dAO.loadCategoryAsync("all").GetAwaiter().GetResult();
                if (categoryList == null)
                    categoryList = new List<String>();
                myUserControl = new IssueAddition(categoryList);
                if (!Dispatcher.CurrentDispatcher.CheckAccess())
                    Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
                    {
                        Globals.ThisAddIn.CustomTaskPanes.Add(myUserControl, "test").Visible = true;

                    }));
                else
                    Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
                    {
                        Globals.ThisAddIn.CustomTaskPanes.Add(myUserControl, "test").Visible = true;

                    }));
            });
        }
c# outlook user-controls vsto customtaskpane
2个回答
0
投票

您很可能在辅助线程上调用该代码。

如果您绝对必须从辅助线程执行此操作,请使用

Dispatcher.Invoke
(其中
Dispatcher
对象是从主线程上的
Dispatcher.CurrentDispatcher
检索到的)。


0
投票

首先,确保传递给Add方法的用户控件实例是从System.Windows.Forms.UserControl类继承的。

以下代码示例演示如何使用

Add(UserControl, String)
方法创建自定义任务窗格。该示例还使用
CustomTaskPane
对象的属性来修改自定义任务窗格的默认外观。

private MyUserControl myUserControl1;
private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    myUserControl1 = new MyUserControl();
    myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1,
        "New Task Pane");

    myCustomTaskPane.DockPosition =
        Office.MsoCTPDockPosition.msoCTPDockPositionFloating;
    myCustomTaskPane.Height = 500;
    myCustomTaskPane.Width = 500;

    myCustomTaskPane.DockPosition =
        Office.MsoCTPDockPosition.msoCTPDockPositionRight;
    myCustomTaskPane.Width = 300;

    myCustomTaskPane.Visible = true;
    myCustomTaskPane.DockPositionChanged +=
        new EventHandler(myCustomTaskPane_DockPositionChanged);
}
© www.soinside.com 2019 - 2024. All rights reserved.