2017-10-11 101 views
0

我們正在開發使用WPF,物料設計和MS-Access 2007作爲後端的應用程序。現在我們面臨着打開單一視圖時應用程序放慢的問題,那個特定的視圖有16個組合的填充。它需要7秒鐘的過程,以下用於綁定組合框項目源在wpf應用程序中打開視圖時的延遲

 List<ComboBind> values = new List<ComboBind>(); 
    try 
    { 
     using (var oleDbCommand = new OleDbCommand()) 
     { 
      oleDbCommand.CommandText = query ; 
      oleDbCommand.Connection = Connection.con; 
      var sql = query; 
      var oleDbDataReader = oleDbCommand.ExecuteReader(); 
     while (oleDbDataReader.Read()) 
      { 
       ComboBind b = new ComboBind(); 
        b.id = oleDbDataReader[0].ToString().ToInt(); 
        b.name = oleDbDataReader[1].ToString(); 
        values.Add(b);     
      }     
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
+0

第1步:刻錄下來'Access'它是純粹的邪惡的源泉! 第2步。發佈你的ComboBox的xaml。 我希望你省略'INotifyPropertyChanged'實現。 – XAMlMAX

+0

即時通訊使用FilteredComboBox派生自組合框類

+0

有兩種可能性:** 1 **您對數據庫的查詢需要很長時間或** 2 **在沒有啓用「Virtualization」的情況下顯示很多記錄,導致應用程序創建大量「U」個項目。現在由你來找出你的情況。你也可以把這些代碼放在你的文章中。 – XAMlMAX

回答

0

它看起來像你想查看在初始化時加載UI線程的數據,這是問題的原因代碼。要麼在後臺線程中加載數據,要麼在打開視圖之前執行它。在單獨的任務

簡單的代碼片段加載數據:

//Might be your view code behind or 
    //ViewModel if you are using MVVM 
    public class ViewCodeBehind 
    { 
     public List<ComboBind> ComboItems { get; set; } 

     public void Initialize() 
     { 
      //start bacground task for data loading 
      var comboQuery = "select *from data"; 
      Task.Run(() => LoadItems(comboQuery)); 
     } 

     public void LoadItems(string query) 
     { 
      List<ComboBind> values = new List<ComboBind>(); 
      try 
      { 
       using (var oleDbCommand = new OleDbCommand()) 
       { 
        oleDbCommand.CommandText = query; 
        oleDbCommand.Connection = Connection.con; 
        var sql = query; 
        var oleDbDataReader = oleDbCommand.ExecuteReader(); 
        while (oleDbDataReader.Read()) 
        { 
         ComboBind b = new ComboBind(); 
         b.id = oleDbDataReader[0].ToString().ToInt(); 
         b.name = oleDbDataReader[1].ToString(); 
         values.Add(b); 
        } 
       } 

       //use dispatcher to pass data back to UI thread 
       System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(
        new Action(() => 
        { 
         ComboItems = values; 
        })); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
    } 
+0

感謝你@Skobarx快速回覆在WPF中新的剛剛開始MVVM,所以你可以解釋我怎麼可以避免 –

+0

@SachinJadhav,你是什麼意思「避免」?如果你的意思是在數據加載期間出現延遲,那麼答案將超出這個話題。 – Skobarx

+0

System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke( new Action(()=> { ComboItems = values; }));偉大的作品... 2秒內打開的視圖謝謝你的幫助 –