2012-06-07 28 views
1

我創建了一個邊界框,並且想要查看它的位置。它被定義爲四個角,我只是想在這四個角之間畫線。應用程序關閉時沒有繪製線條(在調試模式下)

我設法谷歌如何做到這一點:

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 app closes suddenly when it hits a method without throwing any exceptions。建議是它被正確地初始化,並且是我的。 GraphicsDevice作爲參數傳遞並且不是靜態獲得的。

有沒有人知道可能是什麼原因造成的?

感謝,

回答

0

好吧,算出來。代碼如下:

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; 
    } 
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); 


      })); 

它似乎得到了系統崩潰時用戶界面的更改需要而Prcoess仍然是工作..它拋出未處理的異常。

+0

感謝您的回覆。我的對象沒有對Dispatcher的引用,我在哪裏可以找到它以便我可以使用它?我發現它在System.Windows.Threading命名空間下,並在我的文件中添加了一個using語句,但我仍然無法獲取它。謝謝 – Luke

+0

更新:我發現它包含System.Windows DLL並執行Deployment.Current.Dispatcher。 我現在得到一個單一的框架(只要我可以告訴沒有線被繪製),然後它關閉而不提供任何信息。 – Luke

相關問題