2017-08-06 88 views
1

我在Xamarin.Forms這一段簡單的代碼:StackLayout:兒童財產不能分配給

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Xamarin.Forms; 

namespace ReflectedColors 
{ 
    public class ColorBlocksPage : ContentPage 
    { 
     public ColorBlocksPage() 
     { 
      Content = new StackLayout 
      { 

      }; 
     } 
    } 
} 

我點擊cStackLayout成員初始化內部,以設置Children收集和獲取:

enter image description here

,你可以Children是不存在的。

如果我嘗試寫

Content = new StackLayout 
{ 
    Children = new Label { Text = "Test"} 
}; 

試圖建立我的解決方案時,我得到了以下錯誤:

錯誤CS0200屬性或索引「Layout.Children」不能 分配 - - 它是隻讀的

我一定在做一些愚蠢的事,但不幸的是沒有線索可能是什麼來源問題。

+0

檢查例如在https://developer.xamarin.com/api/type/Xamarin.Forms.StackLayout/ – Fruchtzwerg

回答

1

您正在將Children屬性分配給新標籤。
設置兒童屬性的正確方法:

Content = new StackLayout 
{ 
    Children = {new Label { Text = "Test"}} 
}; 
+0

我知道這是愚蠢的東西!我仍然不明白爲什麼IntelliSense不顯示屬性。 –