我创建了一个Observable集合,目的是从数据库中检索记录以便在DataGrid控件中查看。运行应用程序时,在以下代码部分中收到Null Reference异常:
public static async Task<MainViewModel> CreateAsync()
{
var viewModel = new MainViewModel();
viewModel.RecordingDevices = new ObservableCollection<DeviceInformation>();
var outputDevices = await DeviceInformation.FindAllAsync(DeviceClass.AudioRender);
foreach (var device in outputDevices.Where(d => d.IsEnabled))
{
viewModel.OutputDevices.Add(device);
}
//SelectedOutputDevice = OutputDevices.FirstOrDefault(d => d.IsDefault);
var outputId = MediaDevice.GetDefaultAudioRenderId(AudioDeviceRole.Default);
viewModel.SelectedOutputDevice = viewModel.OutputDevices.FirstOrDefault(d => d.Id == outputId);
var inputDevices = await DeviceInformation.FindAllAsync(DeviceClass.AudioCapture);
foreach (var device in inputDevices.Where(d => d.IsEnabled))
{
viewModel.RecordingDevices.Add(device);
}
var defaultId = MediaDevice.GetDefaultAudioCaptureId(AudioDeviceRole.Default);
viewModel.SelectedInputDevice = viewModel.RecordingDevices
.FirstOrDefault(d => d.Id == defaultId);
viewModel.ActivityLogEntries = new ObservableCollection<ActivityLogEntry>();
try
{
if (viewModel.ActivityLogEntries != null)
{
viewModel.ActivityLogEntries = await ActivityLogService.GetActivityLogEntriesAsync();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Activity log error ("+ex.Message+"Details: "+ex.InnerException);
}
return viewModel;
}
如何解决这个空引用,因为它与ActivityLogEntries可观察的集合有关?