SharpDX.Direct2D1.RenderTarget.EndDraw 没有 return HRESULT - 如何检测故障?特别是D2DERR_RECREATE_TARGET?

SharpDX.Direct2D1.RenderTarget.EndDraw doesn't return HRESULT - how detect a failure? Especially D2DERR_RECREATE_TARGET?

将代码从 Microsoft 的旧托管 DirectX 接口移植到 SharpDX。

Microsoft's ID2D1RenderTarget::EndDraw 的文档说它应该 return 一个 HRESULT:

If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code and sets tag1 and tag2 to the tags that were active when the error occurred.

特别重要的是,这是我通常会检测到需要重新创建目标的地方 (D2DERR_RECREATE_TARGET)。事实上,微软在他们的例子中展示了这一点:

hr = m_pRenderTarget->EndDraw();
if (hr == D2DERR_RECREATE_TARGET) ...

SharpDX 的实现是一个子程序 - 没有 return 值。如 github source 所示:

/// <returns>If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code and sets tag1 and tag2 to the tags that were active when the error occurred. </returns>
    public void EndDraw()

评论 仍然提到 HRESULT,但这是 void-returning。

如何查看 HRESULT?

检查 SharpDX 源代码:

    public void EndDraw(out long tag1, out long tag2)
    {
        SharpDX.Result result = TryEndDraw(out tag1, out tag2);
        result.CheckError();
    }

调用:

    public void CheckError()
    {
        if (_code < 0)
        {
            throw new SharpDXException(this);
        }
    }

异常构造函数:

    public SharpDXException(Result result)
        : this(ResultDescriptor.Find(result))
    {
        HResult = (int)result;
    }

设置 SharpDXException 的这个字段:

public int HResult { get; protected set; }

因此,SharpDX 通过抛出 SharpDXException 公开 HRESULT,如果您有 catch (SharpDXException ex) 子句,HRESULT 可读为 ex.HResult


更多信息:
.