Question

after reading some stuff about MVVM I figured it would be a good idea to put my Code where I get the Data in the App.xaml.cs OnStartup method. There I retrieve Appointments from my Exchange. They're filtered via the Date. If the Application is started the first time it should be today. But on my Window I have an DatePicker Control so the User can change the Date. If that happens this whole Code needs to be run again to get the correct Appointments. There are some Problems:

Problem 1:
The Constroctur of my ViewModel Class needs the List of Appointments. So I can't create a new ViewModel before that List is created. But to create the List I need a SelectedDate, wich is Property of the ViewModel.

Problem 2:
How can I let that Code be run again if the SelectedDate changed? Or should I recreate the same Code somewhere and call it there, i.e. in the ViewModel?

App.xaml.cs

public partial class App : Application
{
    private AppointmentOverviewViewModel _vm;

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        service.Credentials = new WebCredentials("user@user.de", "password");
        service.AutodiscoverUrl("user@user.de", RedirectionUrlValidationCallback);

        List<SCSMAppointment> scsmappointments = new List<SCSMAppointment>();

        PropertySet ps = new PropertySet(BasePropertySet.FirstClassProperties);
        ps.RequestedBodyType = BodyType.Text;


        DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        int countDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

        CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, ps);
        CalendarView cView = new CalendarView(firstDay, firstDay.AddDays(countDays));
        FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

        foreach (Appointment a in appointments)
        {
            //code

            scsmappointments.Add(scsmapp);
        }

        _vm = new AppointmentOverviewViewModel(scsmappointments);

        _vm.SelectedDate = DateTime.Today;
        AppointmentOverview window = new AppointmentOverview();
        window.DataContext = _vm;
        window.Show();
    }

AppointmentOverview.xaml

<toolkit:DatePicker x:Name="DatePicker" Grid.Column="3" Grid.Row="0" FirstDayOfWeek="Monday" SelectedDate="{Binding SelectedDate}"/>

AppointmentOverviewViewModel.cs

private DateTime _selectedDate;
    public DateTime SelectedDate
    {
        get { return _selectedDate; }
        set
        {
            if (_selectedDate != value)
            {
                _selectedDate = value;
                NotifyPropertyChanged("SelectedDate");
            }
        }
    }

    public AppointmentOverviewViewModel(List<SCSMAppointment> source)
    {
        _source = source;            
    }
Was it helpful?

Solution

You should move your data access code into the ViewModel, then in the setter of the Selected date you can call the GetAppointments method every time the date is changed. Something like this

private DateTime _selectedDate;
public DateTime SelectedDate
{
    get { return _selectedDate; }
    set
    {
        if (_selectedDate != value)
        {
            _selectedDate = value;
            GetAppointments(_selectedDate);
            NotifyPropertyChanged("SelectedDate");
        }
    }
}

public ObservableCollection<SCSMAppointment> Appointments { get; private set; }

public AppointmentOverviewViewModel()
{
    Appointments = new ObservableCollection<SCSMAppointment>();
    SelectedDate = DateTime.Today;
}

private void GetAppointments(datetime selectedDate)
{
    Appointments.Clear();

    DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
    int countDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

    CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, ps);
    CalendarView cView = new CalendarView(firstDay, firstDay.AddDays(countDays));
    FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

    foreach (Appointment a in appointments)
    {
        Appointments.Add(scsmapp);
    }
}

Then you can remove all of the data access code from your App's OnStartup event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top