昨天我突然出现了一堆错误,我正在努力了解更多关于堆栈跟踪的信息。具体来说,这是否意味着错误发生在的构造函数中
LiveEventPageViewModel
?乍一看,我似乎找不到至少在构造函数中有NRE的地方。
Xamarin Exception Stack:
System.NullReferenceException: Object reference not set to an instance of an object
at Mobile.ViewModels.LiveEventPageViewModel+<>c__DisplayClass155_2.<.ctor>b__25 () <0x104e59740 + 0x00028> in <78675ea96ed449259a925f705a9483ae#82b92fbd1137ac2cb642d6b195f4e27e>:0
at Foundation.NSAsyncActionDispatcher.Apply () <0x1040e2a70 + 0x00023> in <234f9d1731fd4e1ebf977b0a61b8dd61#82b92fbd1137ac2cb642d6b195f4e27e>:0
at (wrapper managed-to-native) UIKit.UIApplication.xamarin_UIApplicationMain(int,string[],intptr,intptr,intptr&)
at UIKit.UIApplication.UIApplicationMain (System.Int32 argc, System.String[] argv, System.IntPtr principalClassName, System.IntPtr delegateClassName) <0x10411b330 + 0x00033> in <234f9d1731fd4e1ebf977b0a61b8dd61#82b92fbd1137ac2cb642d6b195f4e27e>:0
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x10411b400 + 0x0005f> in <234f9d1731fd4e1ebf977b0a61b8dd61#82b92fbd1137ac2cb642d6b195f4e27e>:0
at redacted.iOS.Application.Main (System.String[] args) <0x103298ad0 + 0x00023> in <3d3c1bdeb22740829833318dcbef716d#82b92fbd1137ac2cb642d6b195f4e27e>:0
这是这个特定ViewModel的构造函数。我唯一能想到的就是
app
为null或
app.CurrentUser
但在我加载这个页面之前,已经检查过了。用户已登录到该应用程序,甚至需要登录才能获得此深度。
public LiveEventPageViewModel(long evtId, ContentPage page, bool onSiteMode) {
var app = (Application.Current as App);
SelectedTab = 1;
this.OnSiteMode = onSiteMode;
eventService = new EventService();
LiveEventService = new LiveEventService();
PollService = new PollService();
this.eventId = evtId;
this.page = page;
this.currentUser = app.CurrentUser;
this.currentUserId = app.CurrentUser.Id;
app.AppResumed += App_AppResumed;
Messages = new ObservableCollection<MessageModel>();
Xamarin.Forms.BindingBase.EnableCollectionSynchronization(Messages, null, ObservableCollectionCallback);
Questions = new ObservableCollection<QuestionHistoryViewModel>();
Polls = new ObservableCollection<PollViewModel>();
SendMessageCommand = new Command(async () => { await SendMessage(this.currentUser.FirstName + " " + this.currentUser.LastName, Message); });
UpVoteQuestionCommand = new Command(async (parameter) => { await UpVote(parameter); });
VoteOnPollCommand = new Command(async (parameter) => { await VoteOnPoll(parameter); });
CompleteEventCommand = new Command(async (parameter) => { await CompleteEvent(); });
TapbackIssuedCommand = new Command(async (parameter) => { await TapbackIssued(parameter); });
MarkQuestionAnsweredCommand = new Command(async (parameter) => { await MarkQuestionAnswered(parameter); });
MarkQuestionUnAnsweredCommand = new Command(async (parameter) => { await MarkQuestionUnAnswered(parameter); });
TogglePollVisibilityCommand = new Command(async (parameter) => { await TogglePollVisibility(parameter); });
hubConnection = new HubConnectionBuilder()
.WithUrl(Constants.ServerAddress + "liveeventhub", options => {
options.AccessTokenProvider = () => Task.FromResult(currentUser.AccessToken);
})
.WithAutomaticReconnect(new RetryPolicy()).Build();
hubConnection.Reconnected += HubConnection_Reconnected;
hubConnection.On<long, string, string, long, long>("postChat", (id, name, message, userId, eventId) => {
if (eventId != this.eventId) {
return;
}
Device.BeginInvokeOnMainThread(() => {
Messages.Add(new MessageModel() { Id = id, UserId = userId, User = name, Message = message, IsSystemMessage = false, IsOwnMessage = userId == currentUserId });
this.MessageReceived.Invoke(this, new EventArgs());
});
});
}