2017-08-06 91 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不显示属性。 –