C# 在wpf中实时更新文本框,不提供任何错误且不输出
我尝试了多种方法来实时更新它,因为我正在尝试制作一个简单的基于GUI的秒表。到目前为止,我根本不想让它看起来漂亮,我只是想让程序运行起来。我已经包括了我试图使用的方法,以便更新C# 在wpf中实时更新文本框,不提供任何错误且不输出,c#,wpf,xaml,async-await,dispatcher,C#,Wpf,Xaml,Async Await,Dispatcher,我尝试了多种方法来实时更新它,因为我正在尝试制作一个简单的基于GUI的秒表。到目前为止,我根本不想让它看起来漂亮,我只是想让程序运行起来。我已经包括了我试图使用的方法,以便更新TextBlock。我最后尝试的方法是this.Dispatcher.Invoke(新操作(()=>…方法)。我尝试的另一种方法是使用async wait方法,我已在注释中的代码中包含了该方法 namespace WpfApp3 { /// <summary> /// Interaction l
TextBlock
。我最后尝试的方法是this.Dispatcher.Invoke(新操作(()=>…
方法)。我尝试的另一种方法是使用async wait
方法,我已在注释中的代码中包含了该方法
namespace WpfApp3
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private /*async*/ void start_Click(object sender, RoutedEventArgs e)
{
// await Task.Run(() =>
{
this.Dispatcher.Invoke( new Action(() =>
{
Stopwatch Timer = new Stopwatch();
Timer.Start();
TimeSpan goneby = Timer.Elapsed;
string time = String.Format("{0:00}:{1:00}.{2:00}",
goneby.Minutes, goneby.Seconds,
goneby.Milliseconds / 10);
TextBlock textBlock = new TextBlock();
textBlock.Width = 100;
textBlock.Height = 50;
textBlock.HorizontalAlignment = HorizontalAlignment.Center;
textBlock.VerticalAlignment = VerticalAlignment.Top;
textBlock.Text = time;
}));
// });
}
}
}
}
名称空间WpfApp3
{
///
///Window1.xaml的交互逻辑
///
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
private/*async*/void start\u单击(对象发送方,路由目标)
{
//等待任务。运行(()=>
{
this.Dispatcher.Invoke(新操作(()=>
{
秒表计时器=新秒表();
Timer.Start();
TimeSpan goneby=计时器。已用时间;
string time=string.Format(“{0:00}:{1:00}.{2:00}”,
戈内比,分钟,戈内比,秒,
刚度毫秒/10);
TextBlock TextBlock=新的TextBlock();
textBlock.Width=100;
textBlock.Height=50;
textBlock.HorizontalAlignment=HorizontalAlignment.Center;
textBlock.VerticalAlignment=VerticalAlignment.Top;
Text=时间;
}));
// });
}
}
}
}
以下是XAML,以防需要解决此问题:
<Window x:Name="window1" x:Class="WpfApp3.Window1"
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:WpfApp3"
mc:Ignorable="d"
Title="New Window" Height="300" Width="300">
<Grid>
<Button x:Name="start" Content="Start" HorizontalAlignment="Left" VerticalAlignment="Top" Width="213" Margin="38,181,0,0" Height="50" Click="start_Click"/>
</Grid>
</Window>
您需要创建计时器来更新文本块: 试试这个:
public partial class Window1: Window
{
DispatcherTimer dt = new DispatcherTimer();
Stopwatch sw = new Stopwatch();
string currentTime = string.Empty;
public MainWindow()
{
InitializeComponent();
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
}
void dt_Tick(object sender, EventArgs e)
{
if (sw.IsRunning)
{
TimeSpan ts = sw.Elapsed;
currentTime = String.Format("{0:00}:{1:00}:{2:00}",
ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
YourtextBlock.Text = currentTime;
}
}
private void startbtn_Click(object sender, RoutedEventArgs e)
{
sw.Start();
dt.Start();
}
}
原始资料来源:
试试这个
<Window x:Class="WpfApp3.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:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Name="lblTime" FontSize="50" Margin="149,50,-149.333,-50.333" />
<Button x:Name="start" Content="Start" HorizontalAlignment="Left" VerticalAlignment="Top" Width="213" Margin="149,166,0,0" Height="50" Click="start_Click"/>
</Grid>
</Window>
using System;
using System.Timers;
using System.Windows;
namespace WpfApp3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Timer timer;
TimeSpan time;
public MainWindow()
{
InitializeComponent();
time = new TimeSpan(0);
timer = new Timer();
timer.Interval = 100;
timer.Elapsed += timer_Elapsed;
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
time += new TimeSpan(0, 0, 0, 0, 100);
this.Dispatcher.Invoke(() => {
lblTime.Text = time.ToString(@"hh\:mm\:ss\:ff");
});
}
private void start_Click(object sender, RoutedEventArgs e)
{
if (!timer.Enabled)
{
timer.Start();
start.Content = "Stop";
}
else
{
timer.Stop();
start.Content = "Start";
}
}
}
}
使用制度;
使用系统计时器;
使用System.Windows;
命名空间WpfApp3
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
定时器;
时间跨度时间;
公共主窗口()
{
初始化组件();
时间=新的时间跨度(0);
定时器=新定时器();
计时器。间隔=100;
timer.appeated+=timer\u appeated;
}
私有无效计时器\u已过(对象发送器,ElapsedEventArgs e)
{
时间+=新的时间跨度(0,0,0,0,100);
this.Dispatcher.Invoke(()=>{
lblTime.Text=time.ToString(@“hh\:mm\:ss\:ff”);
});
}
私有无效开始\单击(对象发送者,路由目标)
{
如果(!timer.Enabled)
{
timer.Start();
start.Content=“停止”;
}
其他的
{
timer.Stop();
start.Content=“开始”;
}
}
}
}
使用async/await是一个很好的解决方案。请注意,由于所有代码都在WPF线程上运行,因此不需要同步问题或锁定
using System;
using System.Timers;
using System.Windows;
namespace WpfApp3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
bool running = false; // not running
TimeSpan updateTime = TimeSpan.FromSeconds(0.1);
public MainWindow()
{
InitializeComponent();
}
// Note that async void should only be used for event handlers
private async void start_Click(object sender, RoutedEventArgs e)
{
if (!running)
{
start.Content = "Stop";
var watch = StopWatch.StartNew();
while(running){
var timeSpan = watch.Elapsed;
var message =
$"Time: {timeSpan.Hours}h {timeSpan.Minutes}m " +
"{timeSpan.Seconds}s {timeSpan.Milliseconds}ms";
lblTime.Text = message
// async sleep for a bit before updating the text again
await Task.Delay(updateTime);
}
}
else
{
running = false;
start.Content = "Start";
}
}
}
}
使用系统;
使用系统计时器;
使用System.Windows;
命名空间WpfApp3
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
bool running=false;//未运行
TimeSpan updateTime=TimeSpan.FromSeconds(0.1);
公共主窗口()
{
初始化组件();
}
//请注意,async void应仅用于事件处理程序
私有异步无效开始\u单击(对象发送方,路由目标)
{
如果(!正在运行)
{
start.Content=“停止”;
var watch=StopWatch.StartNew();
(跑步时){
var timeSpan=watch.appeased;
var消息=
$“时间:{timeSpan.Hours}h{timeSpan.Minutes}m”+
“{timeSpan.Seconds}s{timeSpan.miconds}ms”;
lblTime.Text=消息
//在再次更新文本之前,先异步睡眠一段时间
等待任务。延迟(更新时间);
}
}
其他的
{
运行=错误;
start.Content=“开始”;
}
}
}
}
我的简单秒表版本:
MainWindow.xaml
<Window x:Class="Test.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:Test"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:StopwatchManager x:Key="stopwatchManager" />
</Window.Resources>
<Grid>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding Source={StaticResource stopwatchManager}, Path=Stopwatch1.Duration, Mode=OneWay}" VerticalAlignment="Top" Height="27" Width="188"/>
<Button x:Name="btnStart" Content="Start" HorizontalAlignment="Left" Margin="10,42,0,0" VerticalAlignment="Top" Width="53" Click="btnStart_Click"/>
<Button x:Name="btnStop" Content="Stop" HorizontalAlignment="Left" Margin="68,42,0,0" VerticalAlignment="Top" Width="53" Click="btnStop_Click"/>
</Grid>
</Window>
待办事项:
-更改名称空间(“测试”)
-实现“SaveDuration()”
-设置UI更新时间=>新计时器(100);//=0.1秒
-获取多表秒表副本或将其列为列表
public Stopwatch Stopwatch1 { get { return _stopwatch1; } set { _stopwatch1 = value; } }
static Stopwatch _stopwatch1 = new Stopwatch();
您需要使用计时器而不是秒表
Invoke
在主UI线程上运行它。您需要使用BeginInvoke()
以及我对类似问题的回答。您定义了textblock,现在只需将其添加到一个可视元素上即可。
public Stopwatch Stopwatch1 { get { return _stopwatch1; } set { _stopwatch1 = value; } }
static Stopwatch _stopwatch1 = new Stopwatch();