DirectShow.Net F# 中的接口问题

DirectShow.Net interfaces issue in F#

所以我正在查看 DirectShow.Net 的示例代码,特别是他们在 Capture 文件夹示例下的 PlayCap 示例。您可以下载示例 here Its in C#。它通过将对象转换为接口来做一些有趣的事情。当我尝试在 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 转换,看看我是否正确地执行了它。看起来我是,尽管他们有 1 个示例,而且它本质上非常小。

问题是我收到错误:

Type constraint mismatch. The type 'FilterGraph' is not compatible with the type 'IGraphBuilder'

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

进行向下转换

我发现自己遇到了类似的问题,事实证明,在 C# 中,可以将 GUID 属性添加到 COM 接口和 COM 实现,class 将显示为接口的有效实现.但是,F# 类型系统更严格并且不允许这样做。您可以使用 unbox 函数分配它们

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