当我从size函数内部调用Greet时,DisplayAlert会按预期显示警告但是当事件发生后从委托中调用它时,它将使用正确的名称(Greet Has Called)记录到输出但DisplayAlert不会节目。
public class CustomPage : ContentPage
{
...
protected override void OnValidSizeAllocated(double width, double height)
{
...
Greet("Test");
app.FB.GetName();
app.FB.NameRecieved += (s,e) =>
{
Greet(e.Name);
};
}
public void Greet(string name)
{
Utils.Log("Hey " + name);
DisplayAlert("Hey " + name, "Welcome", "OK");
}
}
以上代码输出"嘿测试"然后出现一个警告说"嘿测试,欢迎"然后按下OK按钮输出" Hey Leo" (这是正确的,因为这是来自Facebook帐户的名称)但是没有警报显示。
答案 0 :(得分:12)
在GetName函数中是否触发了NameReceived?
也许您需要在“+ = {...};”之后放置app.FB.GetName()块。
如果正确触发了nameReceived,则可能没有在ui线程上运行Greet, 尝试将您的显示代码包装在
中Device.BeginInvokeOnMainThread (() => {
DisplayAlert("Hey " + name, "Welcome", "OK");
});
如here
所述答案 1 :(得分:2)
Why don't you create a PageBase
class that implements DisplayAlert
and wraps it in BeingInvokeOnMainThread
so that you do not have to keep re-writing it:
public class PageBase : ContentPage
{
public void DisplayAlert(string message,string title, string button)
{
Device.BeginInvokeOnMainThread (() => {
DisplayAlert(message, title, button);
});
}
}