2012-03-25 5 views
0

WPF의 MouseDragElementBehvior에 약간의 문제가 있습니다. 나는 선반에서 보드로 드래그하고자하는 타일이있는 스크래블 게임을 구현하고 있습니다 (둘 다 Canvas 요소 임). 타일은 매번 분명히 다르므로 코드 뒤에 생성됩니다.MouseDragElementBehavior 중복 된 항목 남기기

끌기 동작이 작동하도록 관리하고 타일 랙 (Canvas)에서 다른 캔버스로 이미지를 끌 수 있습니다. 문제는 제가 이사회에 이사를하고 다시 이사하기를 원할 때옵니다. 그것은 처음 몇 번 작동하고 때로는 보드에 이미있는 항목을 끌 때 항목의 복제본을 남긴 다음 null 참조 예외를 throw합니다.

내가 구현 한 방법은 이미지가 현재 드래그되고있는 이미지를 추적하는 글로벌 이미지 변수를 사용하는 것입니다. 기본적으로 MouseDown 이벤트에서 현재 선택된 타일을 현재 선택된 타일로 변경합니다 방금 누르면. 그런 다음

MouseDragElementBehavior dragBe = new MouseDragElementBehavior(); 
dragBe.Attach(imageIcon); 
dragBe.DragFinished += onDragFinishedFromBoard;      
imageIcon.MouseDown += (sender, args) => 
{ 
if (currentTileSelected ==null) 
{ 
     currentTileSelected = sender as Image;        
} }; 

드래그 마무리 트리거됩니다 경우 :

private void onDragFinishedFromBoard(object sender, MouseEventArgs args) 
{ 
if (currentTileSelected != null) 
     { 
      s = (Square) currentTileSelected.Tag; 
      letter = (Tile)s.Letter; 

      double X = args.GetPosition(canvasBoard).X; 
      double y = args.GetPosition(canvasBoard).Y; 
      int row, col; 
      if (X > 0 && y > 0) 
      { 

       row = (int)Math.Floor(y/35); 
       col = (int)Math.Floor(X/35); 
       if (theCurrentGame.TheBoard.ScrabbleBoard[col][row].ContainsLetter==true) 
       { 
        // 
        currentTileSelected.Source = null; 
        currentTileSelected.Visibility = Visibility.Collapsed; 
        currentTileSelected = null; 
        redrawTileRack(); 


       } 
       else 
       { 
        Debug.WriteLine("row: " + row + " col:" + col); 
        //remove it from old position 
        if (s!=null) 
        { 
         s.ContainsLetter = false; 
         s.Letter = null; 
         s.partOfCurrentTurn = false; 
         theCurrentGame.TheBoard.ScrabbleBoard[s.Coordinate.Key][s.Coordinate.Value] = s; 

        } 
        //snap it into board new position 
        Square currentSquare = theCurrentGame.TheBoard.ScrabbleBoard[col][row]; 
        currentSquare.ContainsLetter = true; 
        currentSquare.Letter = letter; 
        currentSquare.partOfCurrentTurn = true; 
        theCurrentGame.TheBoard.ScrabbleBoard[col][row] = currentSquare; 
        drawBoard(theCurrentGame.TheBoard); 
        //remove tile from display 
        theCurrentGame.Human.TileRack.Tiles.Remove(letter); 
        currentTileSelected.Source = null; 
        currentTileSelected.Visibility = Visibility.Collapsed; 
        currentTileSelected = null; 
       } 
      } 
      else 
      { 
       currentTileSelected.Source = null; 
       currentTileSelected.Visibility = Visibility.Collapsed; 
       currentTileSelected= null; 
       redrawTileRack(); 
      } 

     } 

사람이 요소가 끌고 있지만, 뒤 다음 사본을 잎 가져옵니다, 이런 일이 될 수 있습니다 이유에 나를 도울 수 null 참조 예외를 Throw합니까? 또는이를 달성하기위한보다 우수하고 신뢰할 수있는 방법을 제안하십시오.

답변

0

내가 어떻게 고쳐야할지 궁금하다면, 다시 그 위에 끌기 전에 기본적으로 캔버스를 비우는 것을 잊었다. 필요한 모든 것은 다시 그려주기 전에 모든 자식 요소를 제거하는 것이 었습니다.

저는 다른 레이어를 보여준 Snoop이라는 도구를 사용하여 코드가 무엇을하는지 파악할 수있었습니다.