【发布时间】:2018-06-12 07:06:08
【问题描述】:
我想指出我对 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; }
}
正如您在代码 cmets 中看到的,主类中包含的 Console.WriteLine() 工作正常。此外,我可以将我的自定义值分配给另一个类中包含的 TextBlocks。相反,二级类中的Console.WriteLine() 行只是输出了几个零。
我错过了什么?
【问题讨论】:
-
生成 GeneratorWindow 后,这两个值将默认为 0,当您更新它们时,您永远不会输出它们
-
用WriteLine()之类的在GeneratorWindow Print()中写一个方法,在MainWondow的值更新后调用它。
标签: c# class properties