我无法从MainWindow类中的方法访问列表

时间:2019-08-19 18:33:00

标签: c# wpf xaml

我是C#的新手,但我不明白为什么我无法访问MainWindow类中的列表。

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<EqualisationSetting> equalisationSettings = new List<EqualisationSetting>
            {
                new EqualisationSetting { LowerFrequencyBound = 20, UpperFrequencyBound = 250, DecibelRatioChange = 0 },
                new EqualisationSetting { LowerFrequencyBound = 250, UpperFrequencyBound = 4000, DecibelRatioChange = 0}
            };

            ItemsCountroller.ItemsSource = equalisationSettings;
        }

        private void AddFrequencyBoundButton_Click(object sender, RoutedEventArgs e)
        {
            equalisationSettings.add(new EqualisationSetting();
        }
    }

它引发“当前上下文SoundEditor中不存在名称'equalisationSettings'”。

1 个答案:

答案 0 :(得分:2)

这是因为您的变量位于MainWindow()方法内部。试试这个:

public partial class MainWindow : Window
{
    List<EqualisationSetting> equalisationSettings;

    public MainWindow()
    {
        InitializeComponent();

        equalisationSettings = new List<EqualisationSetting>
        {
            new EqualisationSetting { LowerFrequencyBound = 20, UpperFrequencyBound = 250, DecibelRatioChange = 0 },
            new EqualisationSetting { LowerFrequencyBound = 250, UpperFrequencyBound = 4000, DecibelRatioChange = 0}
        };

        ItemsCountroller.ItemsSource = equalisationSettings;
    }

    private void AddFrequencyBoundButton_Click(object sender, RoutedEventArgs e)
    {
        equalisationSettings.add(new EqualisationSetting();
    }
}