我正在尝试添加一个功能,当添加网络表中的新键值对时,它将添加带有新数据的新堆栈面板。但是,在运行代码时,使用断点,代码只是静默停止,没有崩溃或任何事情,只是停止。代码在创建StackPanel对象时停止。
public void ValueChanged(ITable source, string key, Value value, NotifyFlags flags)
{
switch (key)
{
case @"AUTON_OPTIONS":
updateAutonOptions(value.GetStringArray());
break;
case @"POSITION_OPTIONS":
updatePositionOptions(value.GetStringArray());
break;
case @"AUTON_FOUND":
updateAutonFound(value.GetString());
break;
default:
StackPanel formItem = null;
RobotData.Dispatcher.Invoke(() =>
{
formItem = (StackPanel)RobotData.FindName(key);
});
if(formItem == null)
{
StackPanel stackPanel = new StackPanel
{
Name = key,
Orientation = Orientation.Horizontal
}; //new stackpanel for new data type
Label label = new Label
{
Content = key
}; //new label for key name
TextBox textBox = new TextBox
{
IsEnabled = false,
Text = value.GetString()
}; //new textbox for value data
stackPanel.Children.Add(label); //add label to new stackpanel
stackPanel.Children.Add(textBox); //add textbox to new stackpanel
RobotData.Dispatcher.Invoke(() =>
{
RobotData.Children.Add(stackPanel);//add new stackpanel to RobotData stackpanel (declared in xaml)
});
}
else
{
foreach(Object o in ((StackPanel)formItem).Children)
{
if(o is TextBox)
{
((TextBox)o).Text = value.GetString();
break;
}
}
}
break;
}
}
感谢朋友以及上述两位评论者的帮助,我解决了这个问题!谢谢你俩!这是有效的代码:
public void ValueChanged(ITable source, string key, Value value, NotifyFlags flags)
{
switch (key)
{
case @"AUTON_OPTIONS":
updateAutonOptions(value.GetStringArray());
break;
case @"POSITION_OPTIONS":
updatePositionOptions(value.GetStringArray());
break;
case @"AUTON_FOUND":
updateAutonFound(value.GetString());
break;
default:
//StackPanel formItem = null;
RobotData.Dispatcher.Invoke(() =>
{
var formItem = LogicalTreeHelper.FindLogicalNode(RobotData, key);
//});
if (formItem == null)
{
//Dispatcher.Invoke(() =>
//{
StackPanel stackPanel = new StackPanel
{
Name = key,
Orientation = Orientation.Horizontal
}; //new stackpanel for new data type
Label label = new Label
{
Content = key
}; //new label for key name
TextBox textBox = new TextBox
{
IsReadOnly = true,
Text = value.GetObjectValue().ToString()
}; //new textbox for value data
stackPanel.Children.Add(label); //add label to new stackpanel
stackPanel.Children.Add(textBox); //add textbox to new stackpanel
RobotData.Children.Add(stackPanel);//add new stackpanel to RobotData stackpanel (declared in xaml)
//});
}
else
{
foreach(Object o in ((StackPanel)formItem).Children)
{
if(o is TextBox)
{
((TextBox)o).Text = value.GetString();
break;
}
}
}
});
break;
}
}