0

我如何加载 WPF 的窗体,以便在窗体处于公寓状态时一个单独的线程从数据库中获取数据?(可拖动/可移动)

我知道这可以通过从您的主窗体中打开一个新窗口来实现,如下所示:

Private Sub openOrderWindow()
    Dim OrderWindow As Orders = New Orders
    OrderWindow.ShowDialog()
End Sub

Private Sub ButtonImport_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ButtonImport.Click
    Dim iThread As System.Threading.Thread = New System.Threading.Thread(AddressOf openOrderWindow)
    iThread.SetApartmentState(System.Threading.ApartmentState.STA)
    iThread.Start()
    ListViewOrderDetail.Focus()
    ' Me.Close()
End Sub

因为我使用'ShowDialog'

但是在主窗口上我不能调用 ShowDialog

我试过的:

 Private Sub AccessDBFilling()
ListViewDataGrid.ItemsSource = Nothing
    accessdblist = Nothing
accessdblist = accessdb.GetFromAccess()
ListViewDataGrid.ItemsSource = accessdblist
End Sub

 Private Sub refresh()
    ListViewDataGrid.Dispatcher.Invoke(New Action(AddressOf AccessDBFilling))
End Sub

Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    iThread = New System.Threading.Thread(AddressOf refresh)
    iThread.SetApartmentState(System.Threading.ApartmentState.STA)
    iThread.Start()
End Sub

我试过把MyBase.ShowDialog().Show().9000+ 其他东西

我使用单独的线程从数据库加载的唯一好处是我可以立即看到屏幕(相反它是一个透明框,直到它加载)但我不能移动它或最小化它

有没有办法让它在加载时移动?

4

1 回答 1

1

是的。你应该明白你做了什么。

ListViewDataGrid.Dispatcher.Invoke(New Action(AddressOf AccessDBFilling))

UI 控件的调度程序对应于 UI 线程。所以 UI 被冻结,因为您在 UI 线程上完全加载数据。

在这里你应该做什么:

Private Sub AccessDBFilling()
    ListViewDataGrid.ItemsSource = Nothing
    accessdblist = Nothing
    accessdblist = accessdb.GetFromAccess()
    //I'm not sure, but you can try the following lines of code. uncomment one of them.

    //ListViewDataGrid.Dispatcher.Invoke(() => ListViewDataGrid.ItemsSource = accessdblist);
    //ListViewDataGrid.ItemsSource = accessdblist;
End Sub

Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    Action loadFromDb = AccessDBFilling;
    loadFromDb.BeginInvoke();
End Sub

因为我不知道VB,你应该从C#翻译它:)

于 2012-04-27T05:01:55.800 回答