我们如何从Microsoft Office加载项调用Invoke(Delegate)方法? [英] How can we call Invoke(Delegate) method from a Microsoft Office Add-in?
问题描述
问题:我们如何使用 Microsoft Office 2010-2016 VSTO 项目.或者,对于 VSTO
项目,是否有其他替代方案?
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?
背景:我正在尝试实现第三方将Windows窗体应用程序添加到我的
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.).
我已经能够实现其Windows Forms示例应用程序的所有部分,但我的 VSTO
项目中的 VS2019
无法识别的以下代码行除外:
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));
备注: delegate void IsActivatedDelegate();
是在其示例的 Form1.cs
文件中声明的委托,而CheckIfActivated()是一种方法在同一 Form1.cs
文件中声明.
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.
更新:
以下内容摘自第三方示例代码的 Form1.cs
文件,该文件正在调用
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);
}
}
............
........
推荐答案
我希望您出于上下文的考虑而显示代码...至少有几行...我知道这一点.如果你有话
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);
您可以替代:
yd.Invoke(new IsActivatedDelegate(CheckIfActivated));
使用
yd(new IsActivatedDelegate(CheckIfActivated));
这篇关于我们如何从Microsoft Office加载项调用Invoke(Delegate)方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!