微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误System.NullReferenceException:'对象引用未设置为对象的实例在集合视图中再次

如何解决错误System.NullReferenceException:'对象引用未设置为对象的实例在集合视图中再次

嗨,我有一个问题,我有一个收集视图和复选框功能。从复选框(在集合视图中)我想在类的对象中写字段。但是我有错误。我解释一下。我在第一个页面上有3页,在第二个页面上有其他功能我有带有复选框和标签的集合视图在第三页上,我有一个标签可以显示有关电机类型的信息(汽车类)。如果在第二页上,请选中复选框,然后单击按钮转到第三页,则我没有问题。但是如果我要转到第1页(例如查看我选择的汽车标记),则会出现错误

Error System.NullReferenceException: 'Object reference not set to an instance of an object.'

XAML 2-ND PAGE

 <CollectionView x:Name="listview" ItemsSource="{Binding MotorType}"   Margin="0" HeightRequest="220">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>

                            <StackLayout WidthRequest="300" Orientation="Horizontal" >
                                    <CheckBox HorizontalOptions="Start" Color="Black" CheckedChanged="CheckBox_CheckedChanged"
                                    IsChecked="{Binding IsSelected}"
                                    />
                                    <Label Text="{Binding Name}" TextColor="Gray"></Label>
                                </StackLayout>
                         
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

代码隐藏2 ND页

public partial class Car_add_model : ContentPage
    {
       public MainCar main { get; set;}

        public Car_add_model(MainCar s)
        {
            main = s;           
            NavigationPage.SetHasNavigationBar(this,false);
        
            InitializeComponent();
             BindingContext = new CaraddModel(Navigation,main);
        }
        Motor prevIoUsModel;
        private void CheckBox_CheckedChanged(object sender,CheckedChangedEventArgs e)
        {
            if (prevIoUsModel != null)
            {
                prevIoUsModel.IsSelected = false;
            }
            Motor currentModel = ((CheckBox)sender).BindingContext as Motor;
            prevIoUsModel = currentModel;
             main.MotorType = currentModel.Name;        

        }

    }

我那里有错误

enter image description here

我正确理解了这里的错误

currentModel.Name; 

因为我试图给函数中的变量赋值(变量是不同的,我在函数中,在viewmodel中和在后面的代码中创建),但总是有此错误

CaraddModel类:INotifyPropertyChanged {public ObservableCollection MotorType {get;组; }

        public MainCar mainCar { get; set; }
    public Command navigateCommand { get; set; }

        public Command navigateCommandBACK { get; set; }
        public async Task GotoPage2()
        {
         await Navigation.PushModalAsync(new CarView(mainCar));
        }

        public async Task GotoPage1()
        {
          await Navigation.PopModalAsync();
        }
  public CaraddModel(INavigation navigation,MainCar carC)
        {
            mainCar = carC;
            this.Navigation = navigation;
            this.navigateCommand = new Command(async () => await GotoPage2());
            this.navigateCommandBACK = new Command(async () => await GotoPage1());

        ///////////////////////////////////////
        MotorType = new ObservableCollection<Motor>
            {
               new Motor(){Name="Petrol",IsSelected=false },new Motor(){Name="Diesel",IsSelected=false},new Motor(){Name="Gas",new Motor(){Name="Petrol / Gas",new Motor(){Name="Electric",};

            if (!string.IsNullOrEmpty(mainCar.MotorType))
            {
                for (int i = 0; i < MotorType.Count; i++)
                {
                    if (MotorType[i].Name.Equals(mainCar.MotorType)) MotorType[i].IsSelected = true;
                 }

            }
         }

我要在其中写入值的主类

public class MainCar: INotifyPropertyChanged
        {  string motortype;
            public string MotorType
            {
                set
                {
                    if (motortype != value)
                    {
                        motortype = value;
                        OnPropertyChanged("MotorType");
    
                    }
                }
                get
                {
                    return motortype;
                }
    
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
            }
    }

但是,如果我在列表视图中编写此代码,则没有错误,但是我需要使用集合视图来使列表项的3列

我的上一个问题已关闭,因为有人给出了此链接

What is a NullReferenceException,and how do I fix it?

我知道什么是NullReferenceException。我不明白为什么在编写ListView时此代码起作用而在collectionview中不起作用 请不要再关闭我的问题并帮助我

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。