在Appx.xaml.cs OnStartup中重新运行代码

时间:2014-04-30 11:42:24

标签: c# wpf mvvm

在阅读了一些关于MVVM的内容后,我认为将我的代码放在App.xaml.cs OnStartup方法中获取数据的位置是个好主意。在那里,我从我的Exchange检索约会。他们通过日期过滤。如果应用程序在今天第一次启动时启动。但是在我的Window我有DatePicker控件,因此用户可以更改日期。如果发生这种情况,则需要再次运行整个代码才能获得正确的约会。有一些问题:

问题1:
我的ViewModel类的Constroctur需要约会列表。因此,在创建List之前,我无法创建新的ViewModel。但是要创建List我需要一个SelectedDate,它是ViewModel的属性。

问题2:
如果SelectedDate发生变化,我怎样才能再次运行该代码?或者我应该在某处重新创建相同的代码并在那里调用它,即在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;            
    }

1 个答案:

答案 0 :(得分:0)

您应该将数据访问代码移动到ViewModel中,然后在Selected日期的setter中,每次更改日期时都可以调用GetAppointments方法。像这样的东西

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);
    }
}

然后,您可以从App的OnStartup事件中删除所有数据访问代码。