如何访问 - 更新新窗口(C#,WPF) [英] How to access - update new window (C#, WPF)
问题描述
我有一个窗口A,我在foreach循环中处理一个更大的XML文件。
在循环之前我打算创建一个新的进度窗口(B),在循环中我应该能够像处理XY包那样更新窗口B.
任何想法,链接,样本或更简单的方法吗?
感谢任何提示!
我尝试过的事情:
我试图创建一个新线程,也调用窗口B上的文本框,没有运气
I have window A where i process a bigger XML file within a foreach loop.
Before the loop I plan to create a new "progress" window (B), and within the loop I should be able to update window B like XY package was processed.
Any idea, link, sample or easier way to do this?
Thanks for any hint!
What I have tried:
I tried to create a new thread, also invoke the textbox on window B with no luck
推荐答案
最好的办法是另一个圆形。
将所有处理代码移到一个单独的线程中: BackgroundWorker Class(System.ComponentModel)| Microsoft Docs [ ^ ]是一个不错的选择,因为它提供了进度报告而没有任何交叉线程UI问题。
然后使用报告机制来更新进度表单(甚至只是主表单上的进度控件。
该链接包含一个简单示例。
The best way to do this is the other way round.
Move all the "Processing" code into a separate thread: BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^] is a good choice because it provides progress reporting without any cross threading UI problems.
Then use the reporting mechanism to update the progress form (or even just a progress control on your main form.
The link contains a simple example.
请看一下这个样本:
MainWindow.xaml
Please have a look at this sample:
MainWindow.xaml
<Window x:Class="WpfProgressTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfProgressTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="1" Height="30">
<ProgressBar Name="ProgressBar"
Foreground="Blue"></ProgressBar>
<TextBlock Name="TextBlockProgress"
FontFamily="Arial"
FontSize="16"
FontWeight="Bold"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Blue"
Foreground="White"></TextBlock>
</Grid>
<StackPanel Grid.Row="2">
<Button Name="ButtonStart" Width="75" Height="25" Margin="5" Content="Start" Click="ButtonStart_OnClick"></Button>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
MainWindow.xaml.cs
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace WpfProgressTest
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonStart_OnClick(object sender, RoutedEventArgs e)
{
var itemsCount = 10;
ProgressBar.Minimum = 0;
ProgressBar.Maximum = itemsCount;
var task = new Task(() =>
{
for (var i = 0; i < itemsCount; i++)
{
// Just for simulation of some work
Thread.Sleep(500);
Application.Current.Dispatcher.Invoke(() =>
{
ProgressBar.Value++;
TextBlockProgress.Text =
[{ProgressBar.Value} / {ProgressBar.Maximum}];
});
}
});
task.Start();
}
}
}
"[{ProgressBar.Value}/{ProgressBar.Maximum}]"; }); } }); task.Start(); } } }
你必须在任务或线程中完成繁重的XML处理工作。
为了模拟工作量,我添加了500ms的Sleep。
每次迭代后,你必须使用 Application.Current更新进度条和文本
。 Dispatcher.Invoke 。
这是必要的,因为您无法从其他线程访问UI组件。
You must do the heavy work of XML processing in a task or thread.
For simulating the workload I added a Sleep of 500ms.
After each iteration you must update the progress bar and text
by using Application.Current.Dispatcher.Invoke.
This is necessary because you cannot access UI components from a different thread.
这篇关于如何访问 - 更新新窗口(C#,WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!