1
窗口中有一個TreeView。我想在事件中從ItemsSource加載TreeViewItems,同時顯示ProgressRing(MahApps.Metro.Controls)。當WPF中有很多UI更新時,ProgressRing會停滯不前?
但ProgressRing總是卡住。
我使用多線程&調度過程中的ItemsSource負荷,也無法正常工作。
XAML:
...
<Button Name="startButton" Content="Start" Click="Start"/>
<controls:ProgressRing x:Name="p1" IsActive="False"></controls:ProgressRing>
<TreeView x:Name="treeView1"></TreeView>
...
CS:
private void Start(object sender, EventArgs e)
{
TreeViewItem root = new TreeViewItem() { Header = "Root", IsExpanded = true };
treeView1.Items.Add(root);
p1.IsActive = true;
Thread thread = new Thread(() =>
{
// Main work
InitTree(root, "D:\\");
startButton.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,
(ThreadStart)delegate() {
p1.IsActive = false;
});
});
thread.Start();
}
private void InitTree(TreeViewItem item, string dir)
{
string[] dirs = Directory.GetDirectories(dir);
foreach (string subdir in dirs)
{
startButton.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle,
(ThreadStart)delegate()
{
TreeViewItem subItem = new TreeViewItem() { Header = subdir, IsExpanded= true };
item.Items.Add(subItem);
InitTree(subItem, subdir);
});
}
}
我已閱讀&嘗試:
http://msdn.microsoft.com/en-us/library/ms741870(v=vs.110).aspx
專業WPF編程,第31章多線程
能否請您發佈一些代碼,以便我們可以看到的問題是什麼.. C#和XAML –
你確定你沒有其他代碼阻塞UI? – Xela
首先,嘗試'DispatcherPriority.Background'而不是'ApplicationIdle'。它有幫助嗎? – Noseratio