charitht99 perera
charitht99 perera

Reputation: 325

ListView not showing in class file

I am working on Xamarin forms project on visual studio 2017 and I have added a ListView in xaml page but I cannot refer it in xaml.cs ,error saying the name 'listview' does not exist in the current context. i have added xaml below .I will be grateful if any one one can point me where the issue is

<?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>

Upvotes: 0

Views: 27

Answers (2)

Krunal Bagadia
Krunal Bagadia

Reputation: 419

Please follow Below steps.

  1. Open Solution Explorer
  2. Right Click on UserCreationPage.xaml Page & click on Properties.
  3. Set Build Action to EmbededResource

Upvotes: 0

EvZ
EvZ

Reputation: 12169

I would remove the StackLayout around your ListView, it is unnecessary. That's also the reason why the compiler is not happy.

The only ListView I can spot is a ListView with the name usersListView, the compiler is right. So, if you want to access your ListView in code behind (xaml.cs) you have to refer to the right name:

usersListView.Enabled = true;

P.S.: You XAML seems to be extremely messy with unnecessary hierarchy which may lead to performance issues. Try to keep it as flat as possible. There is no need to have a StackLayout within a StackLayout in your DataTemplate.

Upvotes: 1

Related Questions