Mvvm 使用ViewModel、Xamarin.Forms中的绑定在StackLayout中添加子项

Mvvm 使用ViewModel、Xamarin.Forms中的绑定在StackLayout中添加子项,mvvm,xamarin.forms,stacklayout,Mvvm,Xamarin.forms,Stacklayout,我正试图在我的Xamarin.Forms项目中实现MVVM 这是我的带有x:Name的StackLayout <StackLayout x:Name="approvalsStack"></StackLayout> 现在我陷入了困境,如何使用ViewModel/Binding填充子对象。您不能这样做,正如您所看到的,StackLayout中的属性子对象不是一个问题 但是,您可以通过使用bindable属性实现定制一个来解决这个问题,该属性将有一些ItemSource用于动

我正试图在我的Xamarin.Forms项目中实现MVVM

这是我的带有x:Name的
StackLayout

<StackLayout x:Name="approvalsStack"></StackLayout>

现在我陷入了困境,如何使用ViewModel/Binding填充子对象。

您不能这样做,正如您所看到的,
StackLayout
中的属性
子对象
不是一个问题

但是,您可以通过使用bindable属性实现定制一个来解决这个问题,该属性将有一些
ItemSource
用于动态添加/删除其中的项,或者您也可以使用Xamarin.Forms的
RepeaterView
的一些优秀社区实现,我建议您这样做

RepeaterView
基本上是一个可绑定的
StackLayout
,您可以在其中动态填充项目

我建议你看看这个,如果你想你可以在谷歌或github上搜索更多的(但我强烈推荐你这个)

祝你在编码方面好运

找到了解决方案

1-使用此RepeaterView实现 2-向Xaml添加名称空间

<local:RepeaterView x:Name="approvalsStack" ItemsSource="{Binding Approvals}">
                        <local:RepeaterView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <Label Grid.Column="0" Text="{Binding FullName}"/>
                                            <Label Grid.Column="1" Text="{Binding Decision}"/>
                                            <Label Grid.Column="2" Text="{Binding DecisionDate}"/>
                                        </Grid>
                                        <StackLayout>
                                            <BoxView HeightRequest="1" Color="Gray" HorizontalOptions="FillAndExpand"/>
                                        </StackLayout>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </local:RepeaterView.ItemTemplate>
                    </local:RepeaterView>


并绑定repeater视图,然后使用ViewModel将其弹出

是的,我想我应该使用RepeaterView。。实施后会反馈给你的。谢谢,如果你同意,请告诉我。谢谢兄弟。。帮了很多忙:)很高兴听到这个!
<local:RepeaterView x:Name="approvalsStack" ItemsSource="{Binding Approvals}">
                        <local:RepeaterView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <Label Grid.Column="0" Text="{Binding FullName}"/>
                                            <Label Grid.Column="1" Text="{Binding Decision}"/>
                                            <Label Grid.Column="2" Text="{Binding DecisionDate}"/>
                                        </Grid>
                                        <StackLayout>
                                            <BoxView HeightRequest="1" Color="Gray" HorizontalOptions="FillAndExpand"/>
                                        </StackLayout>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </local:RepeaterView.ItemTemplate>
                    </local:RepeaterView>