F#中的DirectShow.Net接口问题

时间:2015-01-15 06:10:15

标签: f# directshow.net c#-to-f#

所以我正在查看DirectShow.Net的这个示例代码,特别是Capture文件夹示例下的PlayCap示例。您可以在C#中下载样本here。它通过将对象转换为接口来完成一些有趣的事情。当我试图在F#中忠实地重新创建它时,对象将无法正确投射。

C#:

    IVideoWindow  videoWindow = null;
    IMediaControl mediaControl = null;
    IMediaEventEx mediaEventEx = null;
    IGraphBuilder graphBuilder = null;
    ICaptureGraphBuilder2 captureGraphBuilder = null;

然后在GetInterfaces()

      this.graphBuilder = (IGraphBuilder) new FilterGraph();
      this.captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
      this.mediaControl = (IMediaControl) this.graphBuilder;
      this.videoWindow = (IVideoWindow) this.graphBuilder;
      this.mediaEventEx = (IMediaEventEx) this.graphBuilder;

所以我为我的代码所做的就是F#:

    let mutable videoWindow:  IVideoWindow = null;
    let mutable mediaControl: IMediaControl  = null;
    let mutable mediaEventEx: IMediaEventEx  = null;
    let mutable graphBuilder: IGraphBuilder  = null;
    let mutable captureGraphBuilder: ICaptureGraphBuilder2  = null;

稍后在GetInterfaces()

        graphBuilder <- new FilterGraph() :>  IGraphBuilder
        captureGraphBuilder <-  new CaptureGraphBuilder2() :> ICaptureGraphBuilder2
        mediaControl <- graphBuilder :> IMediaControl;
        videoWindow <- graphBuilder :> IVideoWindow;
        mediaEventEx <- graphBuilder :> IMediaEventEx;

它看起来像一个忠实的娱乐。甚至没有使用功能风格。我正在查看F#中的msdn投射,看看我是否正确地进行了操作。它看起来像我,虽然他们有一个例子,它的性质极其微小。

问题是我收到错误:

  

类型约束不匹配。类型'FilterGraph'与类型'IGraphBuilder'

不兼容

每个人都有类似的错误。我也尝试使用:?>

进行向下转换

1 个答案:

答案 0 :(得分:2)

我发现自己遇到了类似的问题,结果发现在C#中可以将一个GUID属性添加到COM接口和COM实现中,该类将作为接口的有效实现出现。然而,F#型系统更严格,并不允许这样做。您可以使用unbox功能分配它们

graphBuilder <- unbox (new FilterGraph())
captureGraphBuilder <- unbox (new CaptureGraphBuilder2())