了解component.getService()
-
11-10-2019 - |
题
我有一个代码中的UserControl中的方法
public bool GetActiveDocument(ref EnvDTE.Document doc)
{
try
{
var dte = (DTE2)GetService(typeof(SDTE));
doc = dte.ActiveDocument;
if (doc == null)
{
MessageBox.Show("There isn't any file open in the Code Editor");
return false;
}
else return true;
}
catch (Exception)
{
MessageBox.Show("There was a problem getting the actual file, verify if it is open");
return false;
}
}
我想将此方法移至“代理”类,该类将充当Visual Studio和我的应用程序之间的中介。问题在于,GetService仅在控件内拨打该活动时才正确返回活动文档。当我将该方法移至代理类时,GeterVice没有定义。我搜索此方法来自componentmodel.component,因此我使代理类衍生自组件。一切都可以编译,但是当我要求使用活动文档时,会发生异常。我对我不太了解如何工作的事情getService()方法。请为此提供帮助。
解决方案
component.getService Invokes iserviceProvider.getService在分配给组件的站点属性的ISITE实例上(假设有一个)。您不需要使您的代理实例从组件中继承,但是您需要使其访问ISITE/ISERVICEPROVIDER。
不隶属于 StackOverflow