使用后台线程在TextBlock中更新进度

时间:2013-06-14 12:03:44

标签: c# wpf task-parallel-library

我的WPF窗口有一个封装在ScrollViewer中的TextBlock。

<Window x:Class="WpfScrollProgress.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="545" Width="662"
    Loaded="OnLoad">
    <Grid>
        <Label Content="Please wait while the items are being processed" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="lblStatus" VerticalAlignment="Top" Width="263" />
        <ScrollViewer Name="ScrollViewer1" Margin="10,46,0,136">
            <TextBlock Height="293" HorizontalAlignment="Left" Margin="10,15,10,100" Name="txtStatus" Text="" VerticalAlignment="Top" Width="574" Canvas.Left="-9" />
        </ScrollViewer>
    </Grid>
</Window>

我正在尝试使用后台线程更新进度状态。但是,当在文本块

中更新状态时,滚动操作似乎不起作用
using System;
using System.Windows;
using System.Threading.Tasks;
using System.Threading;

namespace WpfScrollProgress
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private void OnLoad(System.Object sender, System.Windows.RoutedEventArgs e)
        {
            Action<object> oProgressAction = (object s) => { RefreshProgressStatus((string)s); };
            ProgressReporter oProgressReporter = new ProgressReporter(TaskScheduler.FromCurrentSynchronizationContext(), oProgressAction);

            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i <= 100; i++)
                {
                    oProgressReporter.ReportProgress("Hello " + i);
                    Thread.Sleep(100);
                }
            });
        }

        public void RefreshProgressStatus(string strUpdate)
        {
            txtStatus.Text = txtStatus.Text  + strUpdate + "............" + Environment.NewLine;
        }

    }

    public class ProgressReporter
    {

        private TaskScheduler _oScheduler;
        private string _strStatus;

        private Action<object> _oProgressAction;
        public ProgressReporter(TaskScheduler oScheduler, Action<object> oProgressAction)
        {
            _oScheduler = oScheduler;
            _oProgressAction = oProgressAction;
        }

        public TaskScheduler Scheduler
        {
            get { return _oScheduler; }
        }

        private Task ReportProgressAsync(Action<object> action)
        {
            return Task.Factory.StartNew(action, _strStatus, CancellationToken.None, TaskCreationOptions.None, this.Scheduler);
        }

        public void ReportProgress(string strStatus)
        {
            _strStatus = strStatus + "..........." ;
            this.ReportProgressAsync(_oProgressAction).Wait();
        }

    }

}

滚动操作似乎无法在ScrollViewer上运行,并且TextBlock内容不会显示在ScrollViewer之外。这里缺少什么东西?

1 个答案:

答案 0 :(得分:1)

您的TextBlock具有固定的WidthHeight。它不会拉伸/增长超过这个。如果您的文字比Textblock的Width占用的空间更多,则会被裁剪。

删除HeightWidth属性。

我建议您阅读基本的WPF布局/设计概念,以及如何通过DataBinding正确更新UI元素属性,而不是使用过程代码。