gpt4 book ai didi

c# - WPF SelectionChanged 事件在选项卡更改之前执行操作

转载 作者:行者123 更新时间:2023-11-30 17:47:49 32 4
gpt4 key购买 nike

我的 WPF 应用程序中有 SelectionChanged 事件。 i want that when the tab is change to do some action but first i want the tab to visualy change before the action starts.我正在使用后台工作人员来完成这项工作。我的代码是:

    private void Tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tab1.IsSelected)
{
//this line is not working
tabcontrol.SelectedIndex = 1;
wNetTest = new BackgroundWorker();
wNetTest.DoWork += new DoWorkEventHandler(worker_DoWork);
wNetTest.RunWorkerCompleted += worker_RunWorkerCompleted;
wNetTest.WorkerReportsProgress = true;
wNetTest.WorkerSupportsCancellation = true;
wNetTest.RunWorkerAsync();

}
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
//do the job
}

最佳答案

您的问题是您的代码正在同步运行。因此,Tab_SelectionChanged 事件处理程序的每一行都将运行之前您将看到TabItem 更改。要解决此问题,您只需要异步运行长时间运行的进程。最简单的方法之一是:

private void Tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tab1.IsSelected)
{
//this line is not working
tabcontrol.SelectedIndex = 1;
Task.Factory.StartNew(() => LongRunningMethod(parameter));

}
}

private void LongRunningMethod(object parameter)
{
// perform long running process here
}

parameter 输入参数是可选的...如果不需要,请将其删除。

关于c# - WPF SelectionChanged 事件在选项卡更改之前执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24177093/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com