Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin中网格上的属性Getter_C#_Xaml_Xamarin_Properties_Grid - Fatal编程技术网

C# Xamarin中网格上的属性Getter

C# Xamarin中网格上的属性Getter,c#,xaml,xamarin,properties,grid,C#,Xaml,Xamarin,Properties,Grid,如何为包含多个控件的项创建属性 我有一个从网格继承的用户控件。我有一个Add方法来添加子对象。如果我在代码隐藏中创建UserControl并使用Add方法,那么一切都可以正常工作。但是,我希望能够在Xaml中添加到它。所以我做了一个叫做内容的属性。二传手很好,但我不知道该把什么放在接球手上。我尝试过的一切都失败了。我真的不确定我应该得到什么,因为它可以包含多个项目。我的Add方法只是创建行或列定义并执行其他布局功能。为内容中添加的每个项调用Setter(在我的示例中是两次) 我想这样使用它: &

如何为包含多个控件的项创建属性

我有一个从网格继承的用户控件。我有一个Add方法来添加子对象。如果我在代码隐藏中创建UserControl并使用Add方法,那么一切都可以正常工作。但是,我希望能够在Xaml中添加到它。所以我做了一个叫做内容的属性。二传手很好,但我不知道该把什么放在接球手上。我尝试过的一切都失败了。我真的不确定我应该得到什么,因为它可以包含多个项目。我的Add方法只是创建行或列定义并执行其他布局功能。为内容中添加的每个项调用Setter(在我的示例中是两次)

我想这样使用它:

<controls:MyControl>
  <controls:MyControl.Content>
    <Label Text="test" />
    <Label Text="test" />
  </controls:MyControl.Content>
</controls:MyControl>
public IList<View> Children
{
    get { return _children; }
}

// and the constructor
public MyControl() {
    _children = new List<View>();
}


我尝试的所有操作都会出现错误“属性内容为null或不可IEnumerable”

在第一个代码示例中,您描述了一个控件,该控件内部只能包含一个子控件。这就是ContentPage的基本功能。如果查看,它有一个简单的可绑定属性
Content

public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(ContentPage), null, propertyChanged: TemplateUtilities.OnContentChanged);

public View Content
{
    get { return (View)GetValue(ContentProperty); }
    set { SetValue(ContentProperty, value); }
}
您要寻找的是更接近StackLayout的东西,它可以有多个子视图。在堆栈布局中,
Children
ContentProperty
(而不是ContentPage中的
Content
),这意味着在编写此内容时:

<StackLayout>
    <Label>
    <Label>
</StackLayout>

毕竟,不需要set方法。

IDK,如果我清楚我想做什么。我基本上是在尝试使用网格制作我自己类型的StackLayout。我称之为属性内容,但它确实是网格的子元素。当我删除Setter时,我的网格中不再有任何内容(因为我无法调用Add方法)。如何捕获项的添加(Xaml中的项),以便调用Add方法?我不想直接将其添加到继承的网格中,因为我必须添加RowDefs或ColumnDefs并设置大小。如果我在代码中这样做,只需调用我的Add即可,但我想在Xaml中这样做。@Kasper啊,好的,现在就有意义了。如果查看我链接到的布局类,可以看到
onchildeded
OnChildRemoved
方法。当您在链的更高层时,您会注意到类似于
OnInternalAdded
OnInternalRemoved
的方法。可能需要一些工作来解释它们是如何工作的,但这应该是一个良好的开端。
<StackLayout>
    <StackLayout.Children>
        <Label>
        <Label>
    </StackLayout.Children>
</StackLayout>
public IList<T> Children
{
    get { return _children; }
}
public IList<View> Children
{
    get { return _children; }
}

// and the constructor
public MyControl() {
    _children = new List<View>();
}