0

我是 wpf 和 c# 的新手,也是在这里问的。我在网上搜索了几个小时,但我还没有找到解决我的问题的方法。我有一个进度条,可以在循环增加时更新它的值,还有一个更新它的文本的文本块(例如:Reading Lines 5/3000)。目前,我没有绑定路径,所以我在我的 xaml 中进行了虚拟绑定。我不知道如何使它工作。

这是我的 XAML:

<xctk:BusyIndicator x:Name="_busyIndicator" Grid.Row="4">
                <xctk:BusyIndicator.BusyContentTemplate>
                    <DataTemplate>
                        <StackPanel Margin="4">
                            <TextBlock Text="Parsing OTM File" FontWeight="Bold" HorizontalAlignment="Center" />
                            <StackPanel Margin="4">
                                <TextBlock Text="{Binding NumberOfLinesOfStringInTextFile}" />
                                <ProgressBar Grid.Row="4" Name="prgParse" Height="15" Value="{Binding CurrentLineNumberAddedToListView}" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </xctk:BusyIndicator.BusyContentTemplate>
                <xctk:BusyIndicator.OverlayStyle>
                    <Style TargetType="Rectangle">
                        <Setter Property="Fill" Value="#ffffeeee"/>
                    </Style>
                </xctk:BusyIndicator.OverlayStyle>
                <xctk:BusyIndicator.ProgressBarStyle>
                    <Style TargetType="ProgressBar">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </Style>
                </xctk:BusyIndicator.ProgressBarStyle>
                <ListBox x:Name="_listBox" />
            </xctk:BusyIndicator>

我有一个按钮触发读取文本文件中的行(在本例中它有 3000 多行)并将字符串行放置到列表视图中的循环。我想更新我的 xaml 中的文本块,该文本块指出“读取行 300/3000”和进度条指示它的百分比,它在将数据传输到列表视图时将其值更新为 10 或 10%。

这是我背后的代码:

 public partial class MainWindow : Window, INotifyPropertyChanged
    {

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

        }

        private void cmdBrowse_Click(object sender, RoutedEventArgs e)
        {

            var OpenOTM = new Microsoft.Win32.OpenFileDialog();
            OpenOTM.DefaultExt = ".otm";
            OpenOTM.Filter = "OTM Files (*.otm)|*.otm|TEXT Files (*.txt)|*.txt"; 

            if (OpenOTM.ShowDialog().GetValueOrDefault(false))
            {
                txtFilePath.Text = OpenOTM.FileName;
            }
        }

        private void cmdOk_Click(object sender, RoutedEventArgs e)
        {
            WithAdjustments wa = new WithAdjustments();
            BackgroundWorker worker = new BackgroundWorker();

            string[] lines = System.IO.File.ReadAllLines(txtFilePath.Text);
            worker.DoWork += (o, ea) =>
            {
                List<String> listOfString = new List<string>();
                foreach (string lin in lines)
                {
                    listOfString.Add(lin);
                    Thread.Sleep(2);
                }

                Dispatcher.Invoke((Action)(() => _listBox.ItemsSource = listOfString));
            };
            worker.RunWorkerCompleted += (o, ea) =>
            {
                _busyIndicator.IsBusy = false;
            };
            _busyIndicator.IsBusy = true;
            worker.RunWorkerAsync();
        }
    }

提前致谢!我希望有一个人可以帮助我 :)

4

2 回答 2

0

您可以使用后台工作人员报告进度回调更新您的属性 CurrentLineNumberAddedToListView、NumberOfLinesOfStringInTextFile,

worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;

worker.ProgressChanged += (o, e) =>
{
     //Here in e.ProgressPercentage you will get the current line number read you can deduct the textblock text and progressbar value to be set on CurrentLineNumberAddedToListView and NumberOfLinesOfStringInTextFile
}

worker.DoWork += (o, ea) =>
        {
            List<String> listOfString = new List<string>();
            BackgroundWorker worker = o as BackgroundWorker;
            int numberofLine = 0;
            foreach (string lin in lines)
            {
                listOfString.Add(lin);

                o.ReportProgress(++numberofLine);
                Thread.Sleep(2);
            }


            Dispatcher.Invoke((Action)(() => _listBox.ItemsSource = listOfString));
        };
于 2014-07-04T03:36:39.747 回答
-1

也许这可以帮助您的 ProgressBar:

Private Sub InitProgressBar(ByVal DblMaximumValueI As Double)
    'set min and max
    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = DblMaximumValueI
    'set start value
    ProgressBar1.Value = 0
End Sub

Private Sub IncremenetProgressBar_()
    ProgressBar1.Value += 1 'or variable
End Sub

并在下面调用 sub 以显示进度:

Public Sub IncrementProgressBar()
   Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, New Action(AddressOf IncremenetProgressBar_))
End Sub

并更新您的 TextBlock 您可以添加一个模块,例如:

Module ExtensionMethods
'anonymous delegates are not allowed in VB.NET, so we need an empty method
Private Sub EmptyMethod()

End Sub

'refresh for Controls
<Extension()> _
Public Sub Refresh(ByVal uiElement As UIElement)
    uiElement.Dispatcher.Invoke(DispatcherPriority.Render, New Action(AddressOf EmptyMethod))
End Sub

End Module

然后在设置 TextBloxName.Text 后调用 TextBloxName.Refresh

于 2014-07-04T07:46:02.493 回答