如何加载WPF的 Main 形式,以便当表单处于公寓状态时,单独的线程会从数据库中获取数据? (可拖动/可移动)
我知道这可以从你的主表单打开一个新窗口,如下所示:
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+其他内容
我使用单独的线程从数据库加载的唯一好处是我可以立即看到屏幕(反对它是一个透明的盒子,直到它加载)但我不能移动它或最小化它
有没有办法让它在加载时可移动?
答案 0 :(得分: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#中翻译它:)