मैं स्कूल के लिए एक सरल टिक टैक पैर की अंगुली खेल लिख रहा हूँ। असाइनमेंट सी ++ में है, लेकिन शिक्षक ने मुझे चुनौती के रूप में सी # और डब्ल्यूपीएफ का उपयोग करने की अनुमति दी है। मुझे सभी गेम लॉजिक समाप्त हो गए हैं और फॉर्म ज्यादातर पूरा हो गया है, लेकिन मैंने दीवार में भाग लिया है। मैं वर्तमान में एक Label
उपयोग कर रहा हूँ कौन है यह है बारी से संकेत मिलता है, और मैं इसे बदलने के लिए जब एक खिलाड़ी एक वैध कदम बनाता है चाहता हूँ। Applications = Code + Markup के अनुसार, मैं Window
वर्ग के FindName
विधि का उपयोग करने में सक्षम होना चाहिए। हालांकि, यह null
लौटता रहता है। यहां कोड है:FindName लौटने अशक्त
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
इसे नहीं ढूंढ सकता। मैंने पदानुक्रम देखने के लिए स्नूप का उपयोग करने का प्रयास किया है, लेकिन फॉर्म से चुनने के लिए एप्लिकेशन की सूची में दिखाई नहीं देता है। मैंने स्टैक ओवरफ्लो पर खोज की और मुझे should be able to use VisualTreeHelper class मिला, लेकिन मुझे समझ में नहीं आता कि इसका उपयोग कैसे किया जाए।
कोई भी विचार?
मैंने इस कार्यक्रम के लिए निजी सदस्य के साथ जाने के लिए चुना है। स्पष्टीकरण के लिए धन्यवाद, मैं जितनी जल्दी हो सके पूरा पृष्ठ पढ़ूंगा। –