C#:创建数据透视项时出错
本文关键字:出错 透视 数据 创建 | 更新日期: 2023-09-27 18:36:40
PivotItem pivotItem = new PivotItem();
之后,我得到了Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
.应该是什么?我对此很困惑。
法典:
foreach (Source source in sources)
{
PivotItem pivotItem = new PivotItem(); /* At this point it falls. */
pivotItem.Header = source.Name;
pivotItem.Margin = new Thickness(0, -10, 0, 0);
ListView listView = new ListView();
listView.ItemsSource = source.Articles;
listView.ItemTemplate = (DataTemplate)Resources["MainItemTemplate"];
listView.ItemClick += OpenArticle_ItemClick;
listView.SelectionMode = ListViewSelectionMode.None;
listView.IsItemClickEnabled = true;
pivotItem.Content = listView;
pvtMain.Items.Add(pivotItem);
}
根据异常,您似乎正在尝试在 UI 线程以外的线程中创建新PivotItem
。您只能与 UI 线程中的 UI 元素进行交互。
你可能从不是从 UI 事件触发的事件处理程序调用此代码。您应该能够通过使用Dispatcher
切换回 UI 线程来解决此问题:
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
foreach (Source source in sources)
{
PivotItem pivotItem = new PivotItem(); /* At this point it falls. */
pivotItem.Header = source.Name;
pivotItem.Margin = new Thickness(0, -10, 0, 0);
ListView listView = new ListView();
listView.ItemsSource = source.Articles;
listView.ItemTemplate = (DataTemplate)Resources["MainItemTemplate"];
listView.ItemClick += OpenArticle_ItemClick;
listView.SelectionMode = ListViewSelectionMode.None;
listView.IsItemClickEnabled = true;
pivotItem.Content = listView;
pvtMain.Items.Add(pivotItem);
}
});