我想指出我对 C# 完全陌生,我只是在四处测试以掌握这种语言。我想要实现的只是在辅助窗口中控制台打印我在 MainWindow 中设置的几个值。这是包含在 MainWindow 类中的函数,由单击按钮触发。private void ValidationExecuted(object sender, ExecutedRoutedEventArgs eventArgs) { // If the validation was successful, let's open a new window. GeneratorWindow generatorWindow = new GeneratorWindow(); generatorWindow.TextBlockName1.Text = this.tbPoints.Text; generatorWindow.TextBlockName2.Text = this.tbPDC.Text; int.TryParse(this.tbPoints.Text, out int numberOfPoints); int.TryParse(this.tbPDC.Text, out int pdc); // Those lines correctly print the values I've inserted in the TextBoxes. Console.WriteLine(numberOfPoints); Console.WriteLine(pdc); generatorWindow.NumberOfPoints = numberOfPoints; generatorWindow.MainPDC = pdc; generatorWindow.Show(); // Resetting the UI. this.validator = new Validator(); this.grid.DataContext = this.validator; eventArgs.Handled = true; }现在我的辅助窗口:public partial class GeneratorWindow : Window{ /// <inheritdoc /> /// <summary> /// Initializes a new instance of the <see cref="T:ABB_Rapid_Generator.GeneratorWindow" /> class. /// </summary> public GeneratorWindow() { this.InitializeComponent(); // Those lines just print a pair of 0. Console.WriteLine(this.NumberOfPoints); Console.WriteLine(this.MainPDC); } /// <summary> /// Gets or sets the number of points. /// </summary> public int NumberOfPoints { private get; set; } /// <summary> /// Gets or sets the main PDC. /// </summary> public int MainPDC { private get; set; }}正如您在代码注释中看到的,Console.WriteLine()主类中包含的内容正常工作。此外,我可以将我的自定义值分配给包含在其他类中的 TextBlock。相反,Console.WriteLine()二级类中的行只是输出几个零。我错过了什么?
3 回答
HUX布斯
TA贡献1876条经验 获得超6个赞
上面的答案是正确的,但还有一个替代方案。您可以使用 setter 方法,如setNumberOfPoints
,setMainPDC
并在设置值后打印 co 控制台。因此,在ValidationExecuted
您调用一个函数来设置变量时,在设置变量后的该函数中,您将其打印到控制台。但不要忘记从控制台中删除打印constructor
- 3 回答
- 0 关注
- 165 浏览
添加回答
举报
0/150
提交
取消