为什么在我设置 SelectedIndex=0 后,我随后(而不是响应)得到使用 SelectedIndex=4 调用的事件处理程序?我追踪到对调度程序的调用,但我不明白为什么。
计划如下:我有一个页面,其中包含带有各种选项卡的选项卡控件。当我单击某些类型的选项卡时,我想从该页面切换到另一个页面。问题是,当我返回此页面时,我又被循环回另一个页面,而且我不明白为什么。
具体来说,我设置了SelectedIndex=0,然后[外部代码]将SelectedIndex更改为4,导致页面再次被切换出去。
// 在 JobTreePage.xaml 中
<TabControl x:Name="mainTab"
ItemsSource="{Binding}"
SelectionChanged="mainTab_SelectionChanged">
// 在 JobTreePage 中
private void mainTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
e.Handled = true;
if (!ready) return;
var ee = this.TryFindResource("Title");
TabItem x = mainTab.SelectedItem as TabItem;
if (x == null) return;
if (IsJobTab(x)) // index 4 is a job tab
{
if (ready ) // assuming the job selected is 1273.
{
ResetTab();
//mainTab.SelectedItem = AddNew;
mw.moldDataButton_Click(sender, e);
}
ready = true;
}
}
public void ResetTab()
{
ready = false;
Application.Current.Dispatcher.BeginInvoke
((Action)delegate { mainTab.SelectedIndex = 0; }, DispatcherPriority.Send, null);
//mainTab.SelectedIndex = 0;
ready = true;
}
在主窗口中我有两个按钮:
public void moldDataButton_Click(object sender, RoutedEventArgs e)
{
CurrentPage = this.moldDataPage;
moldDataPage.ActivateTab("Project");
// the problem line. problem arises after I click on the Customers button.
Dispatcher.Invoke(new System.Action(() => { }), DispatcherPriority.ContextIdle, null);
Slow_COM_Operation();
}
private void CustomersButton_Click(object sender, RoutedEventArgs e)
{
this.jobTreePage.ResetTab();
CurrentPage = this.jobTreePage;
}
因此,在 ResetTab 中从 CustomersButton_Click() 跟踪程序,SelectedIndex == 0,但完成后我们跳转到 mainTab_SelectionChanged 并看到 SelectedIndex == 4。我不知道是什么迫使 SelectedIndex 为 4。
旁白:为什么我有这条调度线路?因为我试图通过 COM 互操作发送异步请求,以便在不冻结对话框的情况下发生其他一些事情。我不知道如何实现目标,但放入该行至少可以让我刷新页面。