ListView的句柄(x:Name)在后面的代码中为null。 Intellisense在编码时“看到”句柄

问题描述 投票:0回答:1

我得到一个“ System.NullReferenceException:'对象引用未设置为对象的实例。'尝试设置ListView ItemSource时,特别是在“ itemSource.ItemSource = _groupServerList”行。请参见下面的c#代码。在后面的代码中,IntelliSense会“看到” x:Name =“ itemList”。没有编译错误。

我也尝试过<ListView x:Name="itemList" ItemSource="{Binding _groupServerList}"。在这种情况下,我会得到空白页。

任何帮助将不胜感激。

预先感谢。

namespace Hosting.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SvrPickerPage : ContentPage
    {

        public EventHandler SavedGroup;

        private Group _group;
        private ObservableCollection<GroupServer> _groupServerList = new ObservableCollection<GroupServer>();
        private ObservableCollection<Server> _fullServerList = LoadDataBase.ServerList();

        public SvrPickerPage(Group group = null)
        {
            _group = group;

            if (group.ServerCount == 0)
            {
                ObservableCollection<GroupServer> tmpGrpServers = new ObservableCollection<GroupServer>();

                foreach (var server in _fullServerList)
                {
                    var t = new GroupServer { grpServer = server, Server_Name = server.Name, IsChecked = false };
                    tmpGrpServers.Add(t);
                }

                _groupServerList = tmpGrpServers;
            }
            else
            {
                //_groupServerList.Clear();
                _groupServerList = _group.GrpServerList;

                // Add servers to those already in the group
                foreach (var server in _fullServerList)
                {
                    var tbl = _group.GrpServerList.SingleOrDefault(t => t.Server_Name == server.Name);

                    if (tbl == null)
                    {   
                        var t = new GroupServer() { grpServer = server, Server_Name = server.Name, IsChecked = false };
                        _groupServerList.Add(t);
                    }
                }
            }

            **itemList.ItemsSource = _groupServerList;**
            BindingContext = _groupServerList;

        }

与以上代码相对应的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"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Hosting.Views.SvrPickerPage"
             Title="Include/Exclude Servers">


    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="Save_ToolbarItem" IconImageSource="icon.png" Text="Save" Clicked="save_ToolbarItem_Clicked" Order="Primary"/>
        <ToolbarItem x:Name="Cancel_ToolbarItem" IconImageSource="icon.png" Text="Cancel" Clicked="Cancel_ToolbarItem_Clicked" Order="Primary"/>
    </ContentPage.ToolbarItems>

    <RelativeLayout>

        <Grid x:Name="columnHeadings" Padding="5,0,5,0" >

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"/>
                <ColumnDefinition Width="100"/>
            </Grid.ColumnDefinitions>

            <Label  Grid.Column="0" Text="Y/N" Font="Helvetica"  FontSize="Medium" FontAttributes="Bold" 
                                HorizontalOptions="Fill" VerticalOptions="Center" BackgroundColor="DeepSkyBlue" TextColor="White"/>
            <Label  Grid.Column="1" Text="Server" Font="Helvetica"  FontSize="Medium" FontAttributes="Bold" 
                                HorizontalOptions="Fill" VerticalOptions="Center" BackgroundColor="DeepSkyBlue" TextColor="White"/>

        </Grid>

        <ListView x:Name="itemList" SeparatorColor="Black" VerticalScrollBarVisibility="Always"
                RelativeLayout.YConstraint="{ConstraintExpression 
                Type=RelativeToView, 
                ElementName=columnHeadings, 
                Property=Y, 
                Factor=1,
                Constant=30}"
                ItemTapped="serverList_ItemTapped">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Grid Grid.Row="0" Padding="5,0,5,0">

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="50"/>
                                    <ColumnDefinition Width="100"/>
                                </Grid.ColumnDefinitions>

                                <CheckBox x:Name="itemPickerChkBox" Grid.Column="0" IsChecked="{Binding IsChecked}" 
                                          BackgroundColor="white" CheckedChanged="itemPicker_CheckedChanged"/>
                                <Label x:Name="serverName" Grid.Column="1" Text="{Binding Server_Name}" FontSize="Medium" 
                                       FontAttributes="Bold" TextColor="Black" HorizontalOptions="Fill" VerticalOptions="Fill" />

                            </Grid>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </RelativeLayout>
</ContentPage>
c# xamarin nullreferenceexception code-behind xamarin-listview
1个回答
0
投票

您在构造函数中缺少对InitializeComponent()的调用。这是一种加载和初始化XAML中定义的所有元素的方法。

© www.soinside.com 2019 - 2024. All rights reserved.