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

我是 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 中不存在名称‘equalizationSettings’”。


繁华开满天机
浏览 56回答 1
1回答

慕容3067478

这是因为您的变量位于 MainWindow() 方法内部。尝试这个:public partial class MainWindow : Window{&nbsp; &nbsp; List<EqualisationSetting> equalisationSettings;&nbsp; &nbsp; public MainWindow()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; equalisationSettings = new List<EqualisationSetting>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new EqualisationSetting { LowerFrequencyBound = 20, UpperFrequencyBound = 250, DecibelRatioChange = 0 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new EqualisationSetting { LowerFrequencyBound = 250, UpperFrequencyBound = 4000, DecibelRatioChange = 0}&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; ItemsCountroller.ItemsSource = equalisationSettings;&nbsp; &nbsp; }&nbsp; &nbsp; private void AddFrequencyBoundButton_Click(object sender, RoutedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; equalisationSettings.add(new EqualisationSetting();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP