내 WPF 창에 ScrollViewer에 TextBlock이 있습니다.백그라운드 스레드를 사용하여 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 이상 표시되지 않습니다에서 ScrollViewer와 TextBlock의 내용에 작동하는 것 나던 스크롤 기능에 업데이트 될 때 작동하는 것. 여기 뭔가 빠져있어?
나는 이것이 작동하는지 먼저 확인하기 위해 'Dispatcher'를 통해 진행 알림이있는 기본 'BackgroundWorker'를 사용하려고합니다. 나는 또한 이것을 얻지 못한다 ....'Thread.Sleep (100);'. –
Thread.Sleep은 프로세스에서 잠시 멈춤을줍니다. 그것을하지 않고도 어떤 차이를 만들지 않습니다. – Msdnexpert
@ WPF-it. 코드가 작동하고 텍스트 블록이 스크롤 영역까지 업데이트됩니다. 저쪽에 그것은 doesnt한다. 필자는 어떻게 든 WPF 다시 페이지 매김과 관련이 있다고 느낍니다. – Msdnexpert