Я не знаю, что не так с моим кодом и как я могу его решить.C# wpf - ссылка на объект не установлена в экземпляр объекта
public class ExampleViewModel<T> : ViewModelBase where T : IAppointment
{
private Uri appointmentsSource;
private ObservableCollection<T> appointments;
public ICommand AppointmentCreatedCommand { get; set; }
public Uri AppointmentsSource
{
get { return this.appointmentsSource; }
set { this.appointmentsSource = value; }
}
public ExampleViewModel()
{
this.AppointmentCreatedCommand = new DelegateCommand(OnAppointmentCreatedCommandExecute);
}
private void OnAppointmentCreatedCommandExecute(object obj)
{
var createdAppointment = ((AppointmentCreatedEventArgs)(obj)).CreatedAppointment as Appointment;
ObservableAppointmentCollection apps = System.Windows.Markup.XamlReader.Load(File.OpenRead("../../Appointments.xaml")) as ObservableAppointmentCollection;
apps.Add(createdAppointment);
File.WriteAllText("../../Appointments.xaml", System.Windows.Markup.XamlWriter.Save(apps));
string text = File.ReadAllText("../../Appointments.xaml");
text = text.Replace("<Appointment.TimeZone><s:TimeZoneInfo /></Appointment.TimeZone>", " ");
File.WriteAllText("../../Appointments.xaml", text);
}
public ObservableCollection<T> Appointments
{
get
{
if (this.appointments == null)
{
this.appointments = new ObservableCollection<T>(LoadAppointmentsSource(this.AppointmentsSource));
}
return this.appointments;
}
}
protected static IEnumerable<T> LoadAppointmentsSource(Uri appointmentsSource)
{
if (appointmentsSource != null)
{
IEnumerable<T> appointments = Application.LoadComponent(appointmentsSource) as IEnumerable<T>;
return appointments;
}
return Enumerable.Empty<T>();
}
private static DateTime GetStart(T a)
{
return a.Start.Date;
}
}
XAML
<i:Interaction.Triggers>
<i:EventTrigger EventName="AppointmentCreated">
<i:InvokeCommandAction Command="{Binding AppointmentCreatedCommand, Mode=TwoWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
есть ошибка
NullReferenceException был необработанное кодом пользователя
объекта не задана ссылка на экземпляр объекта.
на
var createdAppointment = ((AppointmentCreatedEventArgs)(obj)).CreatedAppointment as Appointment;
каждый раз, когда я создать встречу в моем RadScheduleView
(Telerik), он должен был создать назначение затем записать его в Appointments.xaml
.
проверить, является ли obj нулевым или какой тип, если он не равен нулю. –
является объектом типа НазначениеСозданоEventArgs ?? – User2012384
вы также должны опубликовать код вызывающего абонента OnAppointmentCreatedCommandExecute. Очевидно, что 'obj' является нулевым, но нам нужно выяснить, почему :) –