2011-03-09 118 views
1

我在.xaml中定義了我的按鈕。Windows Phone 7中的按鈕陣列

在MainPage.xaml.cs中,我試圖將它們放入數組中。

myButtons[]={But_1,But_2,But_n....}; 

我收到以下錯誤消息。

A field initializer cannot reference the non-static field, method, or property 'NoteTrainer_.MainPage.But_1' 

如果我把該數組中的MainPage()構造我沒有錯誤,但我不能訪問從我的方法數組。

有點新的C#和Windows電話

感謝您的幫助。

+0

請告訴我們你想要做什麼,因爲按鈕配置聲音很VB6ish給我,我們可能能夠引導您走向更好的解決方案。 – Nate 2011-03-09 16:03:44

+0

我正在做一個測驗。我設置一個按鈕等於數組中的按鈕例如correctButton = myButtons [1]。我試圖把該數組加載,初始化等。謝謝 – james 2011-03-09 16:05:12

回答

3

那麼,首先,該聲明看起來不對。但是,第二件事,編譯器告訴你到底發生了什麼錯誤 - 你試圖引用另一個域的初始化器中的一個域。將被宣佈爲正常:

Button[] myButtons; 

然後在構造你可以使用:

myButtons = new[] { But_1, But_2, ... }; 
+0

感謝,工作得很好。 – james 2011-03-10 22:00:02