簡體   English   中英

C# WPF - 添加到另一個類中的列表

[英]C# WPF - Add to a list in another class

我是 C# 和 WPF 的初學者。 我正在創建一個 PIN 系統,其中有 0-9 位數字。 用戶單擊一個按鈕,例如 1,它會將 1 添加到列表中。 作為這種編程語言和結構的初學者。 我將如何去做,這是我到目前為止所擁有的,目前似乎沒有任何效果。

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);
    }
}

有簡單的按鈕,您單擊一個,它應該添加到列表“pinNumbers”中,但它似乎沒有添加。 謝謝,任何幫助不勝感激!

一些 XAML

<Grid>
   <Button x:Name="PIN_0" Content="0" Click="PIN_0_Click"></Button>
</Grid>

您需要在變量中啟動類並刪除靜態聲明

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM