代码之家  ›  专栏  ›  技术社区  ›  Ken Smith

通过Silverlight/javascript访问ActiveX对象中的属性

  •  1
  • Ken Smith  · 技术社区  · 14 年前

    我已经编写了一个ATL/ActiveX对象,它通过COM接口公开各种属性和方法。我希望能够从Silverlight应用程序访问这些方法和属性。我遇到的问题是,我可以从Silverlight/C访问这些方法,但我还没有找到访问其属性的正确语法。

    换句话说,我的Silverlight C代码如下所示:

    var ax = HtmlPage.Document.CreateElement("object");
    ax.Id = "myControl";
    ax.SetAttribute("style", "width: 1px; height: 1px;");
    ax.SetAttribute("classid", "CLSID:42832F4C-3480-4450-A6B5-156B2EFC408F");
    HtmlPage.Document.Body.AppendChild(ax);
    
    // This works
    ax.Invoke("SomeMethod", "param1", "param2");
    
    // Each of these throw a "Failed to invoke" InvalidOperationException
    ax.Invoke("SomeProperty");
    ax.Invoke("SomeProperty", "propertyValue");
    ax.Invoke("get_SomeProperty");
    ax.Invoke("put_SomeProperty", "propertyValue");
    

    当然,我可以围绕Ax对象编写一个纯的JavaScript包装器,并从Silverlight调用JavaScript函数,我还可以这样做。但如果不需要的话,我宁愿避免编写和维护单独的层。

    有什么建议吗?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Ken Smith    14 年前

    好的,解决办法很明显,我只是不够努力。正确的语法是:

    ax.GetProperty("SomeProperty");
    ax.SetProperty("SomeProperty", "propertyValue");
    

    杜赫。