我正在爲學校寫一個簡單的tic tac toe遊戲。這個任務是用C++編寫的,但是老師讓我有權使用C#和WPF作爲挑戰。我已經完成了所有的遊戲邏輯並且表格大部分完成了,但是我碰到了一堵牆。我目前正在使用Label
來表明是誰輪到它,並且我想在玩家進行有效移動時更改它。根據Applications = Code + Markup,我應該可以使用Window
類的FindName
方法。但是,它不斷返回null
。代碼如下:FindName返回null
public TicTacToeGame()
{
Title = "TicTacToe";
SizeToContent = SizeToContent.WidthAndHeight;
ResizeMode = ResizeMode.NoResize;
UniformGrid playingField = new UniformGrid();
playingField.Width = 300;
playingField.Height = 300;
playingField.Margin = new Thickness(20);
Label statusDisplay = new Label();
statusDisplay.Content = "X goes first";
statusDisplay.FontSize = 24;
statusDisplay.Name = "StatusDisplay"; // This is the name of the control
statusDisplay.HorizontalAlignment = HorizontalAlignment.Center;
statusDisplay.Margin = new Thickness(20);
StackPanel layout = new StackPanel();
layout.Children.Add(playingField);
layout.Children.Add(statusDisplay);
Content = layout;
for (int i = 0; i < 9; i++)
{
Button currentButton = new Button();
currentButton.Name = "Space" + i.ToString();
currentButton.FontSize = 32;
currentButton.Click += OnPlayLocationClick;
playingField.Children.Add(currentButton);
}
game = new TicTacToe.GameCore();
}
void OnPlayLocationClick(object sender, RoutedEventArgs args)
{
Button clickedButton = args.Source as Button;
int iButtonNumber = Int32.Parse(clickedButton.Name.Substring(5,1));
int iXPosition = iButtonNumber % 3,
iYPosition = iButtonNumber/3;
if (game.MoveIsValid(iXPosition, iYPosition) &&
game.Status() == TicTacToe.GameCore.GameStatus.StillGoing)
{
clickedButton.Content =
game.getCurrentPlayer() == TicTacToe.GameCore.Player.X ? "X" : "O";
game.MakeMoveAndChangeTurns(iXPosition, iYPosition);
// And this is where I'm getting it so I can use it.
Label statusDisplay = FindName("StatusDisplay") as Label;
statusDisplay.Content = "It is " +
(game.getCurrentPlayer() == TicTacToe.GameCore.Player.X ? "X" : "O") +
"'s turn";
}
}
這是怎麼回事?我在兩個地方都使用了相同的名稱,但FindName
找不到它。我嘗試過使用Snoop來查看層次結構,但表單不顯示在可供選擇的應用程序列表中。我在StackOverflow上搜索,發現我should be able to use VisualTreeHelper class,但我不明白如何使用它。
任何想法?
我選擇了這個程序,與私人成員一起去。感謝您的解釋,我會盡快閱讀完整頁面。 – 2010-02-06 22:04:08