2016-09-21 55 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

相關問題