Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
Xamarin 类文件中未显示ListView_Xamarin_Xamarin.forms - Fatal编程技术网

Xamarin 类文件中未显示ListView

Xamarin 类文件中未显示ListView,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在visual studio 2017上进行Xamarin forms项目,我在xaml页面中添加了一个ListView,但我无法在xaml.cs中引用它,错误是“ListView”名称在当前上下文中不存在。我在下面添加了xaml。如果有人能告诉我问题所在,我将不胜感激 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

我正在visual studio 2017上进行Xamarin forms项目,我在xaml页面中添加了一个
ListView
,但我无法在xaml.cs中引用它,错误是“ListView”名称在当前上下文中不存在。我在下面添加了xaml。如果有人能告诉我问题所在,我将不胜感激

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App99.UserCreationPage">
    <ContentPage.Content>

        <StackLayout>
            <Grid x:Name="grid">
                <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <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="25" FontSize="10"  VerticalOptions="CenterAndExpand"  HorizontalTextAlignment="Start" Text="Name"/>
                <Entry  x:Name="textName" Grid.Row="0"  Grid.Column="1"  WidthRequest="100" FontSize="10" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding Name}"  />
                <Label x:Name="labelAge" Grid.Row="1"  Grid.Column="0" Margin="25" FontSize="10"  Text="Age" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                <Entry  x:Name="textAge" Grid.Row="1"  Grid.Column="1" WidthRequest="100" FontSize="10" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding Age}" />
                <Label x:Name="labelAddress" Grid.Row="2"  Grid.Column="0" Margin="25" FontSize="10"  Text="Address" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                <Entry  x:Name="textAddress" Grid.Row="2"  Grid.Column="1" WidthRequest="100" FontSize="10" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start"  Text="{Binding Address}" />
                <Label x:Name="labelNICNumber" Grid.Row="3"  Grid.Column="0" Margin="25" FontSize="10" Text="NIC" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                <Entry  x:Name="textNIC" Grid.Row="3"  Grid.Column="1" WidthRequest="100" FontSize="10" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding NIC}" />
                <Button Grid.Row="4"  Grid.Column="1" HeightRequest = "40"   VerticalOptions="CenterAndExpand" HorizontalOptions="Start" Text="Save" Clicked="UserSaveClick" />
            </Grid>
        </StackLayout>
        <StackLayout>
            <ListView x:Name="usersListView"  
            Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="7"
            SeparatorColor="Transparent" BackgroundColor="Transparent" HasUnevenRows="True" IsVisible="false">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Vertical">
                                <StackLayout Orientation="Vertical">

                                    <Label 
                                    WidthRequest="70"
                                    HeightRequest="35"
                                    TextColor="#FFFFFF"
                                    FontFamily="Open Sans"
                                    FontSize="10"          
                                    Text="{Binding Name}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Start"
                                    Margin="60,-35,0,0"/>

                                    <Label 
                                    WidthRequest="70"
                                    HeightRequest="35"
                                    TextColor="#FFFFFF"
                                    FontFamily="Open Sans"
                                    FontSize="10"          
                                    Text="{Binding Age}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Start"
                                    Margin="60,-35,0,0"/>

                                    <Label 
                                    WidthRequest="70"
                                    HeightRequest="35"
                                    TextColor="#FFFFFF"
                                    FontFamily="Open Sans"
                                    FontSize="10"          
                                    Text="{Binding Address}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Start"
                                    Margin="60,-35,0,0"/>


                                    <Label 
                                    WidthRequest="70"
                                    HeightRequest="35"
                                    TextColor="#FFFFFF"
                                    FontFamily="Open Sans"
                                    FontSize="10"          
                                    Text="{Binding NIC}"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Start"
                                    Margin="60,-35,0,0"/>

                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我会删除
列表视图周围的
堆栈布局
,这是不必要的。这也是编译器不高兴的原因

我唯一能发现的
ListView
是一个名为
usersListView
ListView
,编译器是对的。因此,如果您想访问code behind(xaml.cs)中的
列表视图
,您必须使用正确的名称:

usersListView.Enabled = true;

注意:您的XAML看起来非常混乱,不必要的层次结构可能会导致性能问题。尽量使它保持平坦。您的
数据模板中的
StackLayout
中不需要有
StackLayout
,请按照以下步骤操作

  • 打开解决方案资源管理器
  • 右键单击UserCreationPage.xaml页面并单击属性
  • 将构建操作设置为嵌入式资源

  • 不,实际上usersListView是我用来指我提到的坏的Listview的,它应该是usersListView