"The application called an interface that was marshalled for a different thread" 创建 MenuFlyout 时

"The application called an interface that was marshalled for a different thread" when creating MenuFlyout

我在创建 MenuFlyout.I 的新实例时遇到此错误搜索了一下但找不到 anything.Code 很简单:

var tmpFlyout = new MenuFlyout();

我尝试从另一个线程创建这个实例,但这也没有帮助,仍然得到 exception.In 在哪些情况下会弹出此异常?

找到这个答案here

The problem is that your event handler doesn't execute on the UI thread. I think the best way to fix that is to convert your EAP (Event-based Asynchronous Pattern) method to TAP (Task-based Asynchronous Pattern) using TaskCompletionSource:

也许这对你有帮助

Dispatcher.RunAsync 解决了问题。

我尝试使用 Task.Run 方法,但出现问题 persisted.As,我使用 RunAsync 并解决了问题。