0

I don't know whats wrong with my code and how can i solve it.

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>

there is an error

NullReferenceException was unhandled by user code

Object reference not set to an instance of an object.

on

var createdAppointment = ((AppointmentCreatedEventArgs)(obj)).CreatedAppointment as Appointment;

everytime i create appointment in my RadScheduleView (Telerik), it was supposed to create appointment then write it in the Appointments.xaml.

4
  • check if obj is null or what is the type if it is not null. Commented Oct 7, 2015 at 7:10
  • is obj of type AppointmentCreatedEventArgs?? Commented Oct 7, 2015 at 7:10
  • you should also post the code of the caller of OnAppointmentCreatedCommandExecute. Obviously, obj is null, but we need to figure out why :) Commented Oct 7, 2015 at 7:12
  • @FabioSalvalai i put all my code in my ViewModel there, im so stuck here Commented Oct 7, 2015 at 7:24

2 Answers 2

1

Seeing your XAML and the types you are using in your command, I think you are confusing Commands and Events.

Commands are a way to execute a method upon one single predetermined action, usually hitting a button (typically by a click, a tap or a key stroke) whereas events are a way for an object to react to all sorts of conditions. Typically events expose many events, whereas there can be only one single command on a user control.

Now, the big difference here is that on an event, the sender populates and passes an eventArgs object, containing details on the nature of the situation which triggered the event. As for the command, this does not happen. It is possible to pass a parameter to the method which will handle the execution of a command when triggered, but you have to choose that object yourself. The way you choose the object to be passed is by means of a data binding, pretty much like the Command itself is bound, but with the CommandParameter attribute instead of Command.

CommandParameter="{Binding ...}"
Sign up to request clarification or add additional context in comments.

9 Comments

i put the XAML code there. btw there is an error 'DSSchedule.DelegateCommand<Telerik.Windows.Controls.AppointmentCreatedEventArgs>' does not contain a constructor that takes 1 arguments
DelegateCommand accepts an Action<T>. make sure OnAppointmentCreatedCommandExecute matches this by changing the type of obj. (I edited the answer)
change the type to? i need to changet the (object obj)?
Well, yes, to AppointmentCreatedEventArgs. That's the type of the object your CommandParameter binds to, right?
Commands are not events_. Your obj is null because you never tell your implementation of ICommandwhat object to pass to Execute(Object). Read the following blog post to understand more about how Commanding works, especially the part called Passing parameter to the Command (kishore1021.wordpress.com/2011/10/08/…)
|
0

In this line :

var createdAppointment = ((AppointmentCreatedEventArgs)(obj)).CreatedAppointment as Appointment;

Check for null value in obj , and CreatedAppointment. It looks like either of them are not having their values set.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.