[英]C# WPF - Add to a list in another class
I am a beginner in C# and WPF.我是 C# 和 WPF 的初学者。 I am creating a PIN system where you have digits 0-9.
我正在创建一个 PIN 系统,其中有 0-9 位数字。 The user clicks a button for instance 1, and it will add 1 to a list.
用户单击一个按钮,例如 1,它会将 1 添加到列表中。 Being a beginner to this programming language and structure.
作为这种编程语言和结构的初学者。 How would I go about doing so, this is what I have so far and nothing seems to work at the moment.
我将如何去做,这是我到目前为止所拥有的,目前似乎没有任何效果。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class MyVariables
{
public static List<int> pinNumbers = new List<int>();
}
private void PIN_0_Click(object sender, RoutedEventArgs e)
{
MyVariables.pinNumbers.Add(0);
MessageBox.Show("List = " + MyVariables.pinNumbers);
}
private void PIN_1_Click(object sender, RoutedEventArgs e)
{
MyVariables.pinNumbers.Add(1);
}
}
There are simply buttons, you click one and it should add to the list "pinNumbers" but it doesn't seem to be adding.有简单的按钮,您单击一个,它应该添加到列表“pinNumbers”中,但它似乎没有添加。 Thank you, any help with be appreciated!
谢谢,任何帮助不胜感激!
Some XAML一些 XAML
<Grid>
<Button x:Name="PIN_0" Content="0" Click="PIN_0_Click"></Button>
</Grid>
you need to start the class in a variable and remove the static declaration您需要在变量中启动类并删除静态声明
public partial class MainWindow : Window
{
private _myVariables { get; set; }
public MainWindow()
{
InitializeComponent();
_myVariables = new MyVariables()
}
public class MyVariables
{
public List<int> pinNumbers = new List<int>();
}
private void PIN_0_Click(object sender, RoutedEventArgs e)
{
_myVariables.pinNumbers.Add(0);
MessageBox.Show("List = " + _myVariables.pinNumbers);
}
private void PIN_1_Click(object sender, RoutedEventArgs e)
{
_myVariables.pinNumbers.Add(1);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.