How can we call Invoke(Delegate) method from a Microsoft Office Add-in?

非 Y 不嫁゛ 提交于 2021-02-11 14:13:31

问题


Question: How can we use Invoke(...) method in a Microsoft Office 2010-2016 VSTO project. Or, are there any work around, alternatives etc for a VSTO project?

Background: I am trying to implement a source code of a third party Windows Forms application into my Microsoft Office VSTO add-in project. The third party vendor has provided its users a sample/example as a Windows Form project with source code and has asked its users to mimic that code to their other projects (WPF, Office Solutions etc.).

I have been able to implement all parts of their Windows Forms sample app except the following line of code that is not recognized by the VS2019 in my VSTO project:

    Invoke(new IsActivatedDelegate(CheckIfActivated));

Remark: delegate void IsActivatedDelegate(); is a delegate declared in their sample's Form1.cs file and CheckIfActivated() is a method declared in the same Form1.cs file.

Now, we know that the Invoke(...) method is from the Control class of System.Windows.Forms namespace. And, hence, cannot be used in a VSTO project. But there may be an alternative for a VSTO project.

UPDATE:

Following is an excerpt from the Form1.cs file of the third party's sample code that is calling Invoke(...) method:

...........
...........
void mnuActDeact_Click(object sender, EventArgs e)
{
    if (isGenuine)
    {
        // deactivate product without deleting the product key allows the user to easily reactivate
        try
        {
            ta.Deactivate(false);
        }
        catch (ThirdPartyObjectException ex)
        {
            MessageBox.Show("Failed to deactivate: " + ex.Message);
            return;
        }

        isGenuine = false;
        ShowTrial(true);
    }
    else
    {
        // Note: you can launch the ThirdPartyObject wizard or you can create you own interface

        // launch ThirdPartyObject.exe to get the product key from the user, and activate.
        Process TAProcess = new Process
        {
            StartInfo =
            {
                FileName = Path.Combine(
                    Path.GetDirectoryName(Application.ExecutablePath),"ThirdPartyObject.exe")
            },
            EnableRaisingEvents = true
        };

        TAProcess.Exited += p_Exited;
        TAProcess.Start();
    }
}

void p_Exited(object sender, EventArgs e)
{
    // remove the event
    ((Process) sender).Exited -= p_Exited;

    // the UI thread is running asynchronous to ThirdPartyObject closing that's why we can't call CheckIfActivated(); directly
    Invoke(new IsActivatedDelegate(CheckIfActivated));
}

delegate void IsActivatedDelegate();

void CheckIfActivated()
{
    bool isNowActivated = false;

    try
    {
        isNowActivated = ta.IsActivated();
    }
    catch (ThirdPartyObjectException ex)
    {
        MessageBox.Show("Failed to check if activated: " + ex.Message);
        return;
    }

    // recheck if activated
    if (isNowActivated)
    {
        isGenuine = true;
        ReEnableAppFeatures();
        ShowTrial(false);
    }
}
............
........

回答1:


I wish you showed code for context sake... at least a few of the lines...buh I know this. if you have say

YourDelegate yd = new YourDelegate(YourMethod);

you can substitute:

yd.Invoke(new IsActivatedDelegate(CheckIfActivated));

with

yd(new IsActivatedDelegate(CheckIfActivated));


来源:https://stackoverflow.com/questions/61052122/how-can-we-call-invokedelegate-method-from-a-microsoft-office-add-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!