2016-09-21 49 views
0

我在Xamarin.Forms中实现了我自己的WrapLayout。现在页面的构造函数中的DisplayAlert没有显示任何内容。DisplayAlert()不在自定义布局中显示

public WrapLayoutExtendedPage() 
{ 
     InitializeComponent(); 

     wrapLayoutExtended.Children.Add(new CustomLabel() { Text = "Label 1 Test", WidthRequest = 500, BackgroundColor = Color.Green, Length = 3 }); 
     wrapLayoutExtended.Children.Add(new CustomEntry() { WidthRequest = 900, BackgroundColor = Color.Red, Length = 9, InsertBreakAfter = true }); 

     wrapLayoutExtended.Children.Add(new CustomLabel() { Text = "Label 2 Test", WidthRequest = 500, BackgroundColor = Color.Green, Length = 5 }); 
     wrapLayoutExtended.Children.Add(new CustomEntry() { WidthRequest = 900, BackgroundColor = Color.Red, Length = 7, InsertBreakAfter = true }); 

     DisplayAlert("TEST", "TEST", "OK"); 
} 

没有例外或什么。我试图在UI线程上运行它:

Device.BeginInvokeOnMainThread(() => 
{ 
    DisplayAlert("TEST", "TEST", "OK"); 
}); 

它不会改变任何东西。

+0

我想你应该建立一个异步方法,并从构造 –

+0

称之为@NathielPaulino但为什么它在我,因为现在创建的所有其他网页的工作? – greenhoorn

+0

当我想创建一个'DisplayAlert'关闭我的当前课程,我从ContentPage继承,你有其他WrapLayout和工作?或只是在这个特定的页面? –

回答

0

你需要await它,不应该在构造函数中调用。

例子:

public async void OnAppearing() 
{ 
    await DisplayAlert("TEST", "TEST", "OK"); 
} 
+0

我甚至不需要await操作符。为什么它在OnAppearing()中工作?它是否在页面出现之前运行,如果我在构造函数中调用它? – greenhoorn

+1

@greenhoorn即使没有它,你也需要'await'操作符。如果不等待返回“任务”的异步方法,您将会遇到一些非常奇怪且难以追查的问题 – hvaughan3

相关问题