0

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

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

XAML 2-ND 页面

 <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 页后面的代码

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;        

        }

    }

我有一个错误

在此处输入图像描述

正如我正确理解这里的错误

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",IsSelected=false },
                     new Motor(){Name="Petrol / Gas",IsSelected=false },
                       new Motor(){Name="Electric",IsSelected=false },

            };

            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 列

我之前的问题(这个)已关闭,因为有人提供此链接

什么是 NullReferenceException,我该如何解决?

我知道什么是 NullReferenceException。我不明白为什么这个代码在我编写 ListView 时有效并且在 collectionview 中无效 请不要再次关闭我的问题并帮助我

4

0 回答 0