代码之家  ›  专栏  ›  技术社区  ›  Robert Harvey

加载对话框时从后台数据库检索数据

  •  0
  • Robert Harvey  · 技术社区  · 7 年前

    我在wpf应用程序中使用dapper从一个数据库中检索几个组合框列表的数据。我希望在后台进行检索;这样可以防止用户打开第一个组合框时出现小延迟。

    所以我做到了:

    private Task<IEnumerable<T_Program>> _allTapes;
    
    // Binds to combobox ItemsSource
    public IEnumerable<T_Program> Tapes => 
       _allTapes.Result.Where(x => x.Program.Equals(Program));
    

    在我的视图模型的构造函数中:

    _allTapes = _conn.GetAllAsync<T_Program>();
    

    但我没有得到我想要的“绩效改进”。

    将光标悬停在 _allTapes 在调试期间产生以下描述:

    Id = 6722, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
    

    所以看起来达珀的 GetAllAsync 方法在通过检索强制执行查询之前不会实际执行查询。 Result 从任务中。

    我如何得到我想要的后台执行?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Peregrine    7 年前

    您当前正在将任务对象分配给所有磁带。

    尝试

    _allTapes = await _conn.GetAllAsync<T_Program>().ConfigureAwait(false);
    
        2
  •  0
  •   Robert Harvey    7 年前

    所以这是我决定的解决办法…在视图模型构造函数中:

    _allTapes = Task.Run(() => _conn.GetAll<T_Program>());
    

    ISASYNC=真 必须添加到组合框中 ItemsSource 绑定以防止窗体加载阻塞。

    ItemsSource="{Binding Tapes, IsAsync=True}"
    

    原来这只是问题的一部分。为了从组合框中获得良好的性能,我必须向它添加一个虚拟化堆栈面板:

    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
    

    组合中甚至没有那么多项(可能有100项)。