C# 如何从另一个WPF表单设置WPF中的文本框或标签文本
我在更新c#中WPF表单中的C# 如何从另一个WPF表单设置WPF中的文本框或标签文本,c#,wpf,binding,textbox,label,C#,Wpf,Binding,Textbox,Label,我在更新c#中WPF表单中的TextBox文本时遇到问题。我通过编程创建了新表单,并添加了一个标签和一个文本框,还有一个变量temp,它表示我之前创建的某个缓冲区中的某个字符串。但是,当我尝试将文本设置到标签或文本框时,什么都没有发生。但是,我可以以新的形式更改窗口标题 我的代码是: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threadin
TextBox
文本时遇到问题。我通过编程创建了新表单,并添加了一个标签
和一个文本框
,还有一个变量temp,它表示我之前创建的某个缓冲区中的某个字符串。但是,当我尝试将文本设置到标签或文本框时,什么都没有发生。但是,我可以以新的形式更改窗口标题
我的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BufferProba
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
BufferStream bfStream = new BufferStream();
private static Action EmptyDelegate = delegate() { };
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread t = new Thread(SetText);
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
t.Start();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
bfStream.put(tBox.Text.Trim());
tBox.Text = "";
}
public void SetText()
{
Thread.Sleep(5000);
Window myWindow = new Window();
StackPanel stackPanel = new StackPanel { Orientation = Orientation.Vertical };
TextBox tboxForm = new TextBox();
Label szzr = new Label { Content = "" };
stackPanel.Children.Add(szzr);
stackPanel.Children.Add(tboxForm);
myWindow.Content = stackPanel;
List<String> listaStringova = new List<String>();
while (true)
{
Thread.Sleep(5000);
String temp = bfStream.get();
listaStringova.Add(temp);
if (temp != "0")
{
//Console.WriteLine(temp);
myWindow.Title = temp;
szzr.Content = temp;
szzr.Background = new SolidColorBrush(Colors.Orange);
szzr.UpdateLayout();
tboxForm.Text = temp;
myWindow.Show();
}
else {
MessageBox.Show("Jebiga");
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间缓冲区
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
BufferStream bfStream=新的BufferStream();
私有静态操作EmptyDelegate=delegate(){};
公共主窗口()
{
初始化组件();
}
已加载私有无效窗口(对象发送器、路由目标)
{
螺纹t=新螺纹(SetText);
t、 SetApartmentState(ApartmentState.STA);
t、 IsBackground=true;
t、 Start();
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
bfStream.put(tBox.Text.Trim());
tBox.Text=“”;
}
公共无效设置文本()
{
睡眠(5000);
窗口myWindow=新窗口();
StackPanel StackPanel=新StackPanel{Orientation=Orientation.Vertical};
textboxform=newtextbox();
标签szzr=新标签{Content=”“};
stackPanel.Children.Add(szzr);
stackPanel.Children.Add(tboxForm);
myWindow.Content=stackPanel;
List listaStringova=新列表();
while(true)
{
睡眠(5000);
字符串temp=bfStream.get();
listaStringova.Add(温度);
如果(温度!=“0”)
{
//控制台写入线(临时);
myWindow.Title=temp;
szzr.含量=温度;
szzr.Background=新的SolidColorBrush(Colors.Orange);
szzr.UpdateLayout();
tboxForm.Text=temp;
myWindow.Show();
}
否则{
MessageBox.Show(“Jebiga”);
}
}
}
}
}
您需要确保访问文本框的是UI线程。使用Dispatcher.CheckAccess方法,如果返回false,则启动UI线程。您应该能够在web上找到大量包含此信息的示例。您应该在主线程上运行它。
您可以使用Dispatcher执行同样的操作
例如:
System.Windows.Application.Current.Dispatcher.Invoke(new Action(SetText()))