2016-06-11 34 views
-1

直到我爲兩個數組添加代碼之後,應用程序才正常工作。我有一些類級別的字符串:代碼更改後WPF程序將不會加載

string cFileName = "customer.txt"; 
string[] cName = new string[0]; 
string[] cPhone = new string[0]; 

而且我已將此添加到Window_Loaded事件:

private void Window_Loaded_1(object sender, RoutedEventArgs e) 
{ 
    //read file on start 
    //ReadFile(); 
    int counter = 0; 
    string line; 
    StreamReader custSR = new StreamReader(cFileName); 
    line = custSR.ReadLine(); 

    while (custSR.Peek() != -1) 
    { 
     Array.Resize(ref cPhone, cPhone.Length + 1); 
     Array.Resize(ref cName, cName.Length + 1); 

     cPhone[cPhone.Length - 1] = line; 
     cName[cName.Length - 1] = line; 
     counter++; 

     //phoneComboBox.Items.Add(cPhone[cPhone.Length - 1]); 
    } 
    custSR.Close(); 

    for (int i = 0; i < counter; i++) 
    { 
     phoneComboBox.Items.Add(cPhone[i]); 
    } 
    //focus when program starts 
    phoneComboBox.Focus(); 
} 

我之所以//ReadFile()是因爲我試圖把它作爲一個單獨的方法和呼叫到Window_Loaded事件。我也嘗試從循環中輸出組合框。我以前沒有遇到過這種情況,無法弄清楚我所做的事情。

+0

放嘗試捕捉周圍,看看你的例外是無聲 –

+0

@GabrielGM如何才能嘗試捕捉幫助,如果它不會加載?我試過了,但仍然不會加載 – Jaqtaris

+0

您是否檢查過'eventvwr'這個應用的特定錯誤?另外,你是否嘗試在調試器下運行應用程序? – sthotakura

回答

0

我沒有閱讀下一行打破循環。我將此標記爲我的答案,因爲它解決了這個問題,並防止任何人在未來遇到此問題。

while (custSR.Peek() != -1) 
{ 
    Array.Resize(ref cPhone, cPhone.Length + 1); 
    Array.Resize(ref cName, cName.Length + 1); 

    cPhone[cPhone.Length - 1] = line; 
    cName[cName.Length - 1] = line; 
    counter++; 

    line = custSR.ReadLine(); //needed this to read next line 
} 
相關問題