0

目前我的程序似乎正在运行到后台工作人员正在做它应该做的事情,因为 UI 在执行这个耗时的任务时保持响应。虽然我无法弄清楚我应该如何更新 Treeview,但目前我必须关闭应用程序并再次打开它才能看到更新的 Treeview。我知道用户界面在一个单独的线程上,我尝试使用调度程序但没有运气......

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>

这是运行数据库更新的后台代码,带有后台工作程序:

       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 保持响应。任何帮助,将不胜感激。

4

1 回答 1

-1

我与调度员关系密切......这很有效。

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

现在 UI 已刷新!

于 2013-09-25T08:28:34.163 回答