我想指出我对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()
正常工作。此外,我可以将自定义值分配给另一个类中包含的TextBlocks。相反,辅助类中的Console.WriteLine()
行只输出几个零。
我失踪了什么?
答案 0 :(得分:2)
问题在于GeneratorWindow
在构造函数方法中写入控制台,因此在更改值之前输出值 。
您可以真正获得该输出的唯一方法是将值作为构造函数的参数传递,并在执行控制台输出之前设置它们(在构造函数中)。虽然似乎没有任何合理的理由走这条路。
例如:
public GeneratorWindow(int numberOfPoints, int mainPdc)
{
this.InitializeComponent();
this.NumberOfPoints = numberOfPoints;
this.MainPDC = mainPdc;
Console.WriteLine(this.NumberOfPoints);
Console.WriteLine(this.MainPDC);
}
或者,如果您想在设置后看到这些值,则需要将控制台输出移动到另一个功能,在设置值之后调用。
例如,将此功能添加到GeneratorWindow
:
public void OutputValues()
{
Console.WriteLine(this.NumberOfPoints);
Console.WriteLine(this.MainPDC);
}
您可以在其他类中设置值后调用它们:
GeneratorWindow generatorWindow = new GeneratorWindow();
generatorWindow.NumberOfPoints = numberOfPoints;
generatorWindow.MainPDC = pdc;
generatorWindow.OutputValues();
答案 1 :(得分:1)
您可以添加参数-iz构造函数来执行此操作
public partial class GeneratorWindow : Window
{
//Private members
int m_numberOfPoints;
int m_mainPDC;
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:ABB_Rapid_Generator.GeneratorWindow" /> class.
/// </summary>
public GeneratorWindow(int mainPDC,int numberOfPoints)
{
this.InitializeComponent();
this.m_mainPDC = mainPDC;
this.m_numberOfPoints = numberOfPoints;
}
/// <summary>
/// Gets or sets the number of points.
/// </summary>
public int NumberOfPoints
{
get{ return m_numberOfPoints; }
set{ m_numberOfPoints = values; }
}
/// <summary>
/// Gets or sets the main PDC.
/// </summary>
public int MainPDC
{
get{ return m_mainPDC; }
set{ m_mainPDC= values; }
}
public void Print()
{
Console.WriteLine(this.NumberOfPoints);
Console.WriteLine(this.MainPDC);
}
}
这也是一个构造函数,因此只需在
调用它GeneratorWindow generatorWindow = new GeneratorWindow();//here
将辅助窗口调用更改为
generatorWindow = new GeneratorWindow(pdc,numberOfPoints);
generatorWindow.Print();
另外,您的代码没有以良好的方式完成IMO,为什么要设置这样的值?
generatorWindow.TextBlockName1.Text = this.tbPoints.Text;
generatorWindow.TextBlockName2.Text = this.tbPDC.Text;
如果您设置了私有变量,就像上面的示例一样,您可以在同一个类中执行所有转换,打印和接收控制台输出。您只需要调用构造函数和打印方法。
答案 2 :(得分:1)
上面的答案是正确的,但还有另一种选择。
在设置值后,您可以使用诸如setNumberOfPoints
,setMainPDC
之类的setter方法和print co console。因此,在ValidationExecuted
中,您需要一个函数来设置变量,并在设置变量后在该函数中将其打印到控制台。但请不要忘记从constructor