方法中的 DisplayAlert 抛出 HandleTargetEvent:UnhandledException

DisplayAlert within a Method Throws HandleTargetEvent: UnhandledException

我正在尝试从 C# SocketIO 库的方法内部调用 DisplayAlert。无论我如何从方法内部调用 DisplayAlert,它总是导致 'InspectorDebugSession(somenum): HandleTargetEvent: UnhandledException'。即使使用辅助方法在技术上尝试在方法之外进行操作,它仍然不起作用。如果我将 socket.On() 移动到构造函数外部并移动到另一个它仍然会触发的方法中,它仍然不起作用。

public PhoneNumberPage ()
    {
        InitializeComponent ();
        App.socket.On("code_status", async (data) =>
        {
            await DisplayAlert("hi", "hi", "hi");
            if (true)
            {
                await this.DisplayAlert("hi", "hi", "hi");
                displayAlert("hi", "hi", "hi");
            }
            if ((string)data == "timeout")
            {
                Console.WriteLine("!TIMEOUT!");
            }
            else if (!(bool)data)
            {
                displayAlert("Wrong Code", "The code you entered was incorrect!", "Ok");
            }
            else if ((bool)data)
            {
                // save
            }
        });
    }
private async void displayAlert(string title, string info, string accept, string cancel)
    {
        await DisplayAlert(title, info, accept, cancel);
    }

App.socket.On lambda 回调可能不在 UI 线程上。

尝试:

Device.BeginInvokeOnMainThread(async () => await DisplayAlert("hi", "hi", "hi"));