【发布时间】:2008-09-19 22:15:29
【问题描述】:
我正在使用 COM Wrapper 与 Windows Media Player 交互。
它使用 AxHost 以某种方式包裹播放器,对我来说这一切都只是引擎盖下的魔法^^
AxHost.AttachInterfaces 看起来像这样
protected override void AttachInterfaces()
{
try
{
//Get the IOleObject for Windows Media Player.
IOleObject oleObject = this.GetOcx() as IOleObject;
//Set the Client Site for the WMP control.
oleObject.SetClientSite(this as IOleClientSite);
Player = this.GetOcx() as WMPLib.WindowsMediaPlayer;
...
只要我将此 AxHost 托管在 Windows 窗体控件中,一切正常。但我无法在构造函数中连接事件。
这个例子不起作用:
public WMPMediaRating()
{
var remote = new WMPRemote.RemotedWindowsMediaPlayer();
_WMP = remote.Player;
_WMP.MediaChange += new _WMPOCXEvents_MediaChangeEventHandler(_WMP_MediaChange);
}
remote.Player 始终为 null,程序因 NullReferencesException 而崩溃。
AttachInterfaces() 中的代码不知何故仅在窗体绘制后或其他所有操作完成后才会执行。
我尝试手动调用 AttachInterfaces(),但这也不起作用,因为 GetOcx() 没有返回任何内容。
那么如何在没有 Windows 窗体的情况下实例化我的 AxHost 继承控件,以便在控制台应用程序中使用它?
【问题讨论】: