在启动时重新运行Appx.xaml.cs中的代码

本文关键字:cs 代码 xaml Appx 启动 重新运行 | 更新日期: 2023-09-27 18:00:51

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

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

问题2:
如果SelectedDate发生更改,我如何让该代码再次运行?或者我应该在某个地方重新创建相同的代码并在那里调用它,即在ViewModel中?

应用程序.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();
    }

预约概述.xaml

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

约会概览ViewModel.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;            
    }

在启动时重新运行Appx.xaml.cs中的代码

您应该将数据访问代码移动到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);
    }
}

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