后台工作完成后无法更新UI

时间:2013-09-24 10:46:51

标签: wpf user-interface treeview backgroundworker

我的程序目前似乎正在努力工作,背景工作者正在做它应该做的事情,因为UI在执行这个耗时的任务时仍然保持响应。虽然我无法弄清楚我应该如何更新Treeview,但此刻我必须关闭应用程序并再次打开它以使更新的Treeview可见。我知道UI是在一个单独的线程上,我尝试过使用调度程序,但没有运气......

Dispatcher.BeginInvoke(
DispatcherPriority.Input, new Action(() => 
{ 
    //update UI
}));

这是TreeView XAML:

<TreeView x:Name="folderView" Grid.Column="0" Grid.Row="1" BorderThickness="0">
            <TreeViewItem Header="Folders" ItemsSource="{Binding SubFolders, Source={StaticResource RootFolderDataProvider}}" Margin="5"/>

            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type my:FolderView}" ItemsSource="{Binding SubFolders}">
                    <StackPanel Orientation="Horizontal" Name="myPanel">
                        <Image x:Name="img" Width="16" Height="16" Source="Images/FolderClosed.png" />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>

                    <HierarchicalDataTemplate.Triggers>
                        <DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="True">
                            <Setter TargetName="img" Property="Source" Value="Images/FolderOpen.png"/>
                        </DataTrigger>
                    </HierarchicalDataTemplate.Triggers>
                </HierarchicalDataTemplate>    
            </TreeView.Resources>
        </TreeView>

这是使用backgroundworker运行DB Update的Code-Behind:

       private void cmdUpdate_Click(object sender, RoutedEventArgs e)
    {
            if (backgroundWorker_Database.IsBusy != true)
                {
                    backgroundWorker_Database.RunWorkerAsync(qMaker);
                }
            }
    private void databaseWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

                 //start the time-consuming process

        }
    }
    private void databaseWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            //An error was thrown by the DoWork event handler
            MessageBox.Show(e.Error.Message, "An Error Occured");
        }
        else
        {
            MessageBox.Show("Update of Database is Completed!");
        }
        cmdUpdate.IsEnabled = true;
        cmdThumbnails.IsEnabled = true;
        cmdOriginals.IsEnabled = true;
    }

    private void databaseWorker_progressChanged(object sender, ProgressChangedEventArgs e)
    {
        //Update the UI Treeview
    }

这是与PropertyChangedEventHandler一起完成的FolderView代码:

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    public ObservableCollection<FolderView> SubFolders
    {
        get
        {
            if (this.subFolders == null)
            {
                this.subFolders = new ObservableCollection<FolderView>();
                //this.subFolders.Clear();

                DirectoryInfo[] di = this.folder.GetDirectories();

                for (int i = 0; i < di.Length; i++)
                {
                    FolderView newFolder = new FolderView();
                    newFolder.FullPath = di[i].FullName;
                    this.subFolders.Add(newFolder);
                }
            }
            return this.subFolders;
        }
        set
        {
            this.subFolders = value; OnPropertyChanged("SubFolders");
        }
    }

再一次,程序线程似乎工作正常,因为在执行耗时的任务时,UI仍然保持响应。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:-1)

我和Dispatcher很接近...... 这很有效。

private void databaseWorker_DoWork(object sender, DoWorkEventArgs e)
{
        ....
        Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
        {
            folderView.Items.Refresh();
            folderView.UpdateLayout();
        }));
        ....

现在UI已刷新!