我正在使用Visual Studio 2017上的Xamarin表单项目,我在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>
答案 0 :(得分:1)
我会删除StackLayout
周围的ListView
,这是不必要的。这也是编译器不满意的原因。
我能发现的唯一ListView
是ListView
,名称为usersListView
,编译器是正确的。因此,如果您想在后面的代码中访问ListView
(xaml.cs),您必须引用正确的名称:
usersListView.Enabled = true;
P.S。:您的XAML似乎非常混乱,不必要的层次结构可能导致性能问题。尽量保持平坦。 StackLayout
中的StackLayout
内无需DataTemplate
。
答案 1 :(得分:0)
请按照以下步骤操作。