绘制线条时,应用关闭时没有信息(在调试模式下)

本文关键字:信息 调试 模式 应用 绘制 | 更新日期: 2023-09-27 18:36:26

我正在创建一个边界框,为了调试,我想看看它在哪里。它被定义为四个角,我只想在这 4 个角之间画线。

我设法谷歌如何做到这一点:

    public void Draw(GraphicsDevice graphicsDevice)
    {
        int num = mCorners.Length;
        VertexPositionColor[] primitiveList = new VertexPositionColor[num];
        for (int i = 0; i < num; ++i)
        {
            primitiveList[i] = new VertexPositionColor(new Vector3(mCorners[i], 0), Color.White);
        }
        short[] triangleStripIndices = new short[] { 0, 1, 2, 3, };
        graphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, primitiveList, 0, 4, triangleStripIndices, 0, 3);
    }

但是,当运行此代码时,应用会关闭。它处于调试模式,但没有堆栈跟踪、崩溃消息、错误日志等。它只是关闭,使其很难调试。

我发现了一个类似的问题,但没有答案:XNA 4.0 应用程序在命中某个方法时会突然关闭,而不会引发任何异常。建议是它是否正确初始化,是的,我的是。图形设备作为参数传递,而不是静态获取。

有谁知道可能导致这种情况的原因吗?

谢谢

绘制线条时,应用关闭时没有信息(在调试模式下)

嗨,

卢克尝试像这样将代码放入调度程序中

   this.Dispatcher.BeginInvoke(new System.Action(delegate()
            {
int num = mCorners.Length;
        VertexPositionColor[] primitiveList = new VertexPositionColor[num];
        for (int i = 0; i < num; ++i)
        {
            primitiveList[i] = new VertexPositionColor(new Vector3(mCorners[i], 0), Color.White);
        }
        short[] triangleStripIndices = new short[] { 0, 1, 2, 3, };
        graphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, primitiveList, 0, 4, triangleStripIndices, 0, 3);

            }));

当UI需要更改并且Prcoess仍在工作时,系统似乎崩溃了。并且它会引发未经处理的异常。

好的,想通了。代码如下:

    private BasicEffect mBasicEffect;
    public void Draw(GraphicsDevice graphicsDevice)
    {
        // If we haven't set this up yet then do so now
        if (mBasicEffect == null)
        {
            CreateBasicEffect(graphicsDevice);
        }
        foreach (EffectPass pass in mBasicEffect.CurrentTechnique.Passes)
        {
            pass.Apply();
            int num = mCorners.Length + 1;
            short[] triangleStripIndices = new short[num];
            VertexPositionColor[] primitiveList = new VertexPositionColor[num];
            for (int i = 0; i < num; ++i)
            {
                int index = i % mCorners.Length;
                Vector2 vec = mCorners[index];
                primitiveList[index] = new VertexPositionColor(new Vector3(vec, 0), Color.White);
                triangleStripIndices[i] = (short)i;
            }
            graphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, primitiveList, 0, 5, triangleStripIndices, 0, 4);
        }
    }
    private void CreateBasicEffect(GraphicsDevice device)
    {
        mBasicEffect = new BasicEffect(device);
        mBasicEffect.VertexColorEnabled = true;
        Matrix viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 1), Vector3.Zero, Vector3.Up);
        Matrix worldMatrix = Matrix.CreateTranslation(0, 0, 0);
        Matrix projectionMatrix = Matrix.CreateOrthographicOffCenter(0, device.Viewport.Width, device.Viewport.Height, 0, 1, 1000);
        mBasicEffect.World = worldMatrix;
        mBasicEffect.View = viewMatrix;
        mBasicEffect.Projection = projectionMatrix;
    }