gpt4 book ai didi

c# - FindName 返回 null

转载 作者:太空狗 更新时间:2023-10-29 22:55:59 25 4
gpt4 key购买 nike

我正在为学校编写一个简单的井字游戏。作业是用 C++ 编写的,但老师允许我使用 C# 和 WPF 作为挑战。我已经完成了所有的游戏逻辑并且表单也基本完成了,但是我遇到了困难。我目前正在使用 Label 来指示轮到谁了,我想在玩家进行有效移动时更改它。根据Applications = Code + Markup ,我应该能够使用 Window 类的 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 ,但我不明白如何使用它。

有什么想法吗?

最佳答案

FindName 对调用控件的 XAML 名称范围进行操作。在您的情况下,由于控件完全是在代码中创建的,因此 XAML 名称范围是空的——这就是 FindName 失败的原因。参见 this page :

Any additions to the element tree after initial loading and processing must call the appropriate implementation of RegisterName for the class that defines the XAML namescope. Otherwise, the added object cannot be referenced by name through methods such as FindName. Merely setting a Name property (or x:Name Attribute) does not register that name into any XAML namescope.

解决问题的最简单方法是将对 StatusDisplay 标签的引用作为私有(private)成员存储在类中。或者,如果您想学习如何使用 VisualTreeHelper 类,可以使用代码片段 at the bottom of this page遍历可视化树以找到匹配的元素。

(已编辑:当然,如果您不想存储对标签的引用,那么调用 RegisterName 比使用 VisualTreeHelper 更省事。)

如果您打算深入使用 WPF/Silverlight,我建议您完整阅读第一个链接。有用的信息。

关于c# - FindName 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2214737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com