我使用的是COM包装与Windows媒体播放器进行交互。
在它被使用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总是空,程序与NullReferencesException崩溃。
该表格已经绘就之后,不知何故只执行在AttachInterfaces的代码(),或一切之后完成。
我试着打电话AttachInterfaces()手,但没有工作,要么因为GetOcx()返回任何内容。
所以,我怎么可以实例我AxHost继承的控制,而Windows窗体,在控制台应用程序使用它的例子吗?
FYI:没有人阻止你在你的控制台应用程序使用一个隐藏的窗口。
你将不能够承载在非Windows应用程序中的媒体播放器 - 它需要托管。如果你要玩一些音乐,你可以使用媒体图创建渲染图(次)音乐文件 - 它会不会需要任何额外的托管。