在wpf应用程序中打开视图时延迟

时间:2017-10-11 05:06:52

标签: wpf mvvm ms-access-2007

我们正在使用WPF,材料设计和MS-Access 2007开发应用程序作为后端。现在我们在打开单个视图时面临应用程序减速问题,特定视图有16个组合填充。以下代码用于绑定组合框项目源

,需要7秒的时间
     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);
     }

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试在视图初始化期间在UI线程中加载数据,这就是问题的原因。要么在后台线程中加载数据,要么在打开视图之前执行此操作。

用于在单独任务中加载数据的简单代码段:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

   SingleChatList chat=chatList.get(listPosition);
   switch (holder.getItemViewType()) {
      case 0:
          incoming incommindHolder= (incoming ) holder;
          //TODO Bind data view Holder
          incommindHolder.message.setText(chat.getMsg());
          incommindHolder.time.setText(String.valueOf(chat.getTimestamp()));
          break;

      case 1:
          outgoing outGoindHolder= (outgoing) holder;
          //TODO Bind data view Holder
          outGoindHolder.message.setText(chat.getMsg());
          outGoindHolder.time.setText(String.valueOf(chat.getTimestamp()));
          break;
   }
}