我正在开发一个多线程应用程序,为每个新线程打开一个新的“可关闭”选项卡。我从this site得到可关闭tabitems的代码,但我也希望在tabitem中有一个文本框。我厌倦了在运行时从main方法中添加文本框,但它不能从之后创建的线程访问。做这项工作的最佳方式是什么?我正在寻找将文本框添加到可从其他工作线程编辑的可关闭选项卡的最佳方式。关闭tabitem。如何添加文本框?
编辑: 我已经添加了一些示例代码来显示我想要实现的。
namespace SampleTabControl
{
public partial class Window1 : Window
{
public static Window1 myWindow1;
public Window1()
{
myWindow1 = this;
InitializeComponent();
this.AddHandler(CloseableTabItem.CloseTabEvent, new RoutedEventHandler(this.CloseTab));
}
private void CloseTab(object source, RoutedEventArgs args)
{
TabItem tabItem = args.Source as TabItem;
if (tabItem != null)
{
TabControl tabControl = tabItem.Parent as TabControl;
if (tabControl != null)
tabControl.Items.Remove(tabItem);
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
Worker worker = new Worker();
Thread[] threads = new Thread[1];
for (int i = 0; i < 1; i++)
{
TextBox statusBox = new TextBox();
CloseableTabItem tabItem = new CloseableTabItem();
tabItem.Content = statusBox;
MainTab.Items.Add(tabItem);
int index = i;
threads[i] = new Thread(new ParameterizedThreadStart(worker.start));
threads[i].IsBackground = true;
threads[i].Start(tabItem);
}
}
}
}
而这是Worker类。
namespace SampleTabControl
{
class Worker
{
public CloseableTabItem tabItem;
public void start(object threadParam)
{
tabItem = (CloseableTabItem)threadParam;
Window1.myWindow1.Dispatcher.BeginInvoke((Action)(() => { tabItem.Header = "TEST"; }), System.Windows.Threading.DispatcherPriority.Normal);
//Window1.myWindow1.Dispatcher.BeginInvoke((Action)(() => { tabItem.statusBox.Text //statusbox is not accesible here= "THIS IS THE TEXT"; }), System.Windows.Threading.DispatcherPriority.Normal);
while (true)
{
Console.Beep();
Thread.Sleep(1000);
}
}
}
}
在我注释掉的行中,statusBox不可访问。
最好的问候!
是的,我知道调度程序,但是当我在运行时将文本框添加到可关闭选项卡时,它无法访问。我将添加一些示例代码,以便您可以看到。 – Arya
“TextBox t = tabItem.Content as TextBox;”使运行时程序崩溃。 – Arya
我刚刚检查了错误消息,这就是它所说的“调用线程不能访问此对象,因为不同的线程拥有它。”你能告诉我如何解决它吗? – Arya