gpt4 book ai didi

c# - ObservableCollection 对象引用

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:08 26 4
gpt4 key购买 nike

我正在尝试了解在 Xamarin Forms 中的不同 View 之间传递数据的基础知识。

我有一个包含可观察集合的 session 类。在后面的代码中,我有另一个 ObservableCollection,用作 listviewItemSource。后面代码中的 ObservableCollection 工作正常,但是当我尝试设置 myMeeting.MeetingInvitees = _myMeetingInvitees 时,我收到此消息:

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

在后面代码的构造函数中,我有:

myMeeting = this.BindingContext as Meeting;

这不会初始化 myMeeting 吗? Topic 和 Place 属性工作正常,只是集合不行。所以当我这样做时:

myMeeting.MeetingInvitees = _myMeetingInvitees;

这不会将它们设置为相同的地址吗?

我觉得在设置 MeetingInvitees

属性的 Meeting 类中有一些我不理解的基本内容

谢谢你能给我指出正确的方向!

session .cs:

private ObservableCollection<String> meetingInvitees;
private string meetingTopic;
private string meetingPlace;

public ObservableCollection<string> MeetingInvitees
{
get { return meetingInvitees; }
set
{
meetingInvitees = value;
}
}
public string MeetingTopic
{
get { return meetingTopic; }
set
{
meetingTopic = value;
OnPropertyChanged("MeetingTopic");
}
}
public string MeetingPlace
{
get { return meetingPlace; }
set
{
meetingPlace = value;
OnPropertyChanged("MeetingPlace");
}
}

Xamarin 代码隐藏

public Meeting myMeeting;
public ObservableCollection<string> _myMeetingInvitees;

public NewMeetingContactsView ()
{
InitializeComponent ();
_myMeetingInvitees = new ObservableCollection<string>();
myMeeting = this.BindingContext as Meeting;
listAttendees.ItemsSource = _myMeetingAttendess;
}

async void btnNext_Clicked(object sender, EventArgs e)
{
myMeeting.MeetingInvitees = _myMeetingAttendess;
var nextpage = new NewMeetingOverviewView();
nextpage.BindingContext = myMeeting;
await Navigation.PushAsync(nextpage);
}

async void btnCancel_Clicked(object sender, EventArgs e)
{
await Navigation.PopToRootAsync();
}

void btnAdd_Clicked(object sender, EventArgs e)
{
_myMeetingAttendess.Add(entryName.Text);
}

最佳答案

在你的构造函数中,而不是做

_myMeetingInvitees = new ObservableCollection<string>();

初始化您的 myMeeting 对象后将其更改为以下代码。

public NewMeetingContactsView ()
{
InitializeComponent ();
myMeeting = new Meeting();
myMeeting.MeetingInvites = new ObservableCollection<string>();
this.BindingContext = myMeeting;
listAttendees.ItemsSource = myMeeting.MeetingInvites;
}

您似乎没有为 myMeeting 初始化您的属性 MeetingInvites,这就是引发空引用错误的原因。

关于c# - ObservableCollection 对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48966202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com