Stacklayout

时间:2018-04-05 05:49:35

标签: c# xamarin xamarin.forms

我'我在Xamarin表单应用程序中工作,我目前正在实现一个表单包含两部分,一部分是数据输入部分,另一部分是列表视图Afetr i输入数据并保存它应该出现在列表视图中,在调试中我检查了ListViewUsers。 ItemsSource count不是0,它有值,但listView没有显示在表单中。

 <ContentPage.Content>
        <StackLayout>
            <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="500"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
             <StackLayout Grid.Row="0" Grid.Column="0">
            <Grid x:Name="grid">
                <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
             </Grid.ColumnDefinitions>
                <Label x:Name="labelName" Grid.Row="0"  Grid.Column="0" Margin="10" FontSize="6"  VerticalOptions="CenterAndExpand"  HorizontalTextAlignment="Start" Text="Name"/>
                <Entry  x:Name="textName" Grid.Row="0"  Grid.Column="1"  WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding Name}"  />
                <Label x:Name="labelAge" Grid.Row="1"  Grid.Column="0" Margin="10" FontSize="6"  Text="Age" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                <Entry  x:Name="textAge" Grid.Row="1"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding Age}" />
                <Label x:Name="labelAddress" Grid.Row="2"  Grid.Column="0" Margin="10" FontSize="6"  Text="Address" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                <Entry  x:Name="textAddress" Grid.Row="2"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start"  Text="{Binding Address}" />
                <Label x:Name="labelNICNumber" Grid.Row="3"  Grid.Column="0" Margin="10" FontSize="6" Text="NIC" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                <Entry  x:Name="textNIC" Grid.Row="3"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding NIC}" />
                <Button Grid.Row="4"  Grid.Column="1" HeightRequest = "30"   VerticalOptions="CenterAndExpand" HorizontalOptions="Start" FontSize="6" Text="Save" Clicked="UserSaveClick" />
            </Grid>
        </StackLayout>
        <StackLayout Grid.Row="1"  Grid.Column="0">
                    <Label x:Name="labelNICNumber2" Grid.Row="3"  Grid.Column="0" Margin="10" FontSize="6" Text="NIC" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                    <ListView x:Name="ListViewUsers" SeparatorColor="Transparent"  HasUnevenRows="True" IsVisible="True" IsEnabled="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="1*" />
                                        <ColumnDefinition Width="2*" />
                                        <ColumnDefinition Width="3*" />
                                        <ColumnDefinition Width="4*" />
                                    </Grid.ColumnDefinitions>



                                    <Label 
                                    WidthRequest="20"
                                    HeightRequest="15"
                                    TextColor="Black"
                                    FontFamily="Open Sans"
                                    FontSize="6"          
                                    Text="{Binding Name}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Start"
                                    Grid.Column="1"/>

                                    <Label 
                                    WidthRequest="20"
                                    HeightRequest="15"
                                    TextColor="Black"
                                    FontFamily="Open Sans"
                                    FontSize="10"          
                                    Text="{Binding Age}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Start"
                                    Grid.Column="2" />

                                     <Label 
                                    WidthRequest="20"
                                    HeightRequest="15"
                                    TextColor="Black"
                                    FontFamily="Open Sans"
                                    FontSize="10"          
                                    Text="{Binding Address}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Start"
                                     Grid.Column="3" />


                                    <Label 
                                    WidthRequest="20"
                                    HeightRequest="15"
                                    TextColor="Black"
                                    FontFamily="Open Sans"
                                    FontSize="10"          
                                    Text="{Binding NIC}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Start"
                                    Grid.Column="4"/>
                                </Grid>

                            </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
        </Grid>
     </StackLayout>

1 个答案:

答案 0 :(得分:1)

很难看到这里发生了什么,但是

  • 我删除了冗余列定义
  • 我删除了最高级别的stacklayout,只使用了一个网格
  • 我删除了第二个嵌套的stacklayout并离开了网格
  • 我已在主网格中将两行设置为*,只是为了确保
  

注意:如果这不起作用,请删除除ListView之外的所有内容,就像进行完整性检查一样

<强>的Xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid x:Name="grid" Grid.Row="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label
            x:Name="labelName"
            Grid.Row="0"
            Grid.Column="0"
            Margin="10"
            FontSize="6"
            HorizontalTextAlignment="Start"
            Text="Name"
            VerticalOptions="CenterAndExpand" />
        <Entry
            x:Name="textName"
            Grid.Row="0"
            Grid.Column="1"
            FontSize="6"
            HorizontalOptions="Start"
            HorizontalTextAlignment="Start"
            Text="{Binding Name}"
            VerticalOptions="CenterAndExpand"
            WidthRequest="100" />
        <Label
            x:Name="labelAge"
            Grid.Row="1"
            Grid.Column="0"
            Margin="10"
            FontSize="6"
            HorizontalTextAlignment="Start"
            Text="Age"
            VerticalOptions="CenterAndExpand" />
        <Entry
            x:Name="textAge"
            Grid.Row="1"
            Grid.Column="1"
            FontSize="6"
            HorizontalOptions="Start"
            HorizontalTextAlignment="Start"
            Text="{Binding Age}"
            VerticalOptions="CenterAndExpand"
            WidthRequest="100" />
        <Label
            x:Name="labelAddress"
            Grid.Row="2"
            Grid.Column="0"
            Margin="10"
            FontSize="6"
            HorizontalTextAlignment="Start"
            Text="Address"
            VerticalOptions="CenterAndExpand" />
        <Entry
            x:Name="textAddress"
            Grid.Row="2"
            Grid.Column="1"
            FontSize="6"
            HorizontalOptions="Start"
            HorizontalTextAlignment="Start"
            Text="{Binding Address}"
            VerticalOptions="CenterAndExpand"
            WidthRequest="100" />
        <Label
            x:Name="labelNICNumber"
            Grid.Row="3"
            Grid.Column="0"
            Margin="10"
            FontSize="6"
            HorizontalTextAlignment="Start"
            Text="NIC"
            VerticalOptions="CenterAndExpand" />
        <Entry
            x:Name="textNIC"
            Grid.Row="3"
            Grid.Column="1"
            FontSize="6"
            HorizontalOptions="Start"
            HorizontalTextAlignment="Start"
            Text="{Binding NIC}"
            VerticalOptions="CenterAndExpand"
            WidthRequest="100" />
        <Button
            Grid.Row="4"
            Grid.Column="1"
            Clicked="UserSaveClick"
            FontSize="6"
            HeightRequest="30"
            HorizontalOptions="Start"
            Text="Save"
            VerticalOptions="CenterAndExpand" />
    </Grid>

    <StackLayout Grid.Row="1">
        <Label
            x:Name="labelNICNumber2"
            Grid.Row="3"
            Grid.Column="0"
            Margin="10"
            FontSize="6"
            HorizontalTextAlignment="Start"
            Text="NIC"
            VerticalOptions="CenterAndExpand" />
        <ListView
            x:Name="ListViewUsers"
            HasUnevenRows="True"
            IsEnabled="True"
            IsVisible="True"
            SeparatorColor="Transparent">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*" />
                                <ColumnDefinition Width="2*" />
                                <ColumnDefinition Width="3*" />
                                <ColumnDefinition Width="4*" />
                            </Grid.ColumnDefinitions>



                            <Label
                                Grid.Column="1"
                                FontFamily="Open Sans"
                                FontSize="6"
                                HeightRequest="15"
                                HorizontalOptions="Start"
                                Text="{Binding Name}"
                                TextColor="Black"
                                VerticalOptions="Center"
                                WidthRequest="20" />

                            <Label
                                Grid.Column="2"
                                FontFamily="Open Sans"
                                FontSize="10"
                                HeightRequest="15"
                                HorizontalOptions="Start"
                                Text="{Binding Age}"
                                TextColor="Black"
                                VerticalOptions="Center"
                                WidthRequest="20" />

                            <Label
                                Grid.Column="3"
                                FontFamily="Open Sans"
                                FontSize="10"
                                HeightRequest="15"
                                HorizontalOptions="Start"
                                Text="{Binding Address}"
                                TextColor="Black"
                                VerticalOptions="Center"
                                WidthRequest="20" />


                            <Label
                                Grid.Column="4"
                                FontFamily="Open Sans"
                                FontSize="10"
                                HeightRequest="15"
                                HorizontalOptions="Start"
                                Text="{Binding NIC}"
                                TextColor="Black"
                                VerticalOptions="Center"
                                WidthRequest="20" />
                        </Grid>

                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</Grid>