ListView 的句柄 (x:Name) 在代码隐藏中为 null。智能感知在编码时“看到”句柄

Handle (x:Name) to ListView is null in Code behind. Intellisense "sees" Handle while coding

提问人:Jay D See 提问时间:4/7/2020 最后编辑:oguz ismailJay D See 更新时间:2/3/2021 访问量:115

问:

尝试设置 ListView ItemSource 时,我收到“System.NullReferenceException: 'Object reference not set to an instance of an object.'。具体来说,在“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 代码隐藏

评论

0赞 Jason 4/7/2020
构造函数中的第一行应该是对 的调用。你删除了吗?InitializeComponent();
0赞 Jay D See 4/7/2020
另一个切和过去的 fop aux !我从我编码的另一个页面复制了这段代码。不知何故,被放弃了。我只是把它加回去,它就起作用了。非常感谢。我在这个问题上浪费了将近一天的时间。InitializeComponent()
0赞 nevermore 5/8/2020
您能否标记正确的答案,这将帮助更多有相同问题的人:)。

答:

0赞 Jason 4/7/2020 #1

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