Prism Event Aggregator不使用Async / Await

时间:2017-05-25 15:08:58

标签: prism

我正在使用sql server在客户端服务器应用程序中使用prism。我在登录屏幕后有一个加载屏幕,我在主窗口的异步任务中获取主数据并发布并在主窗口中订阅,如客户,用户等。但问题是订户未更新。但如果我退出并再次登录,则订阅者会更新。 订阅者未第一次更新。 这是加载屏幕的代码

public class StartPageViewModel : ViewModelBase
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly IEventAggregator _eventAggregator;
    private readonly IRegionManager _regionManager;
    public List<Customer> Customers { get; set; }
    private List<Models.User> Users { get; set; }
    private List<Invoice> Invoices { get; set; }
    public List<Vehicle> Vehicles { get; set; }
    public List<Tax> Taxes { get; set; }
    private Models.User _user;
    private object _userId;

    public StartPageViewModel(IRegionManager regionManager, Models.User user,
        IUnitOfWork unitOfWork, IEventAggregator eventAggregator)
    {
        _regionManager = regionManager;
        _user = user;
        _unitOfWork = unitOfWork;
        _eventAggregator = eventAggregator;
        Customers = new List<Customer>();
        Users = new List<Models.User>();
        Invoices = new List<Invoice>();
        Vehicles = new List<Vehicle>();
        Taxes = new List<Tax>();
    }
    [OnCommand("InitializeStartPage")]
    public async void InitializeStartPage()
    {
        if (!InternetCs.IsConnectedToInternet())
            _regionManager.RequestNavigate(RegionNames.ShellRegion, "InternetConnection");
        else
        {
           await Task.WhenAll(CheckUser(), Preloading()).ContinueWith(async(t, _) =>
           {
               _eventAggregator.GetEvent<AllCustomersEvent>().Publish(Customers);
               _eventAggregator.GetEvent<AllInvoicesEvent>().Publish(Invoices);
               _eventAggregator.GetEvent<CurrentUserEvent>().Publish(_user);
               _eventAggregator.GetEvent<AllUsersEvent>().Publish(Users);
               _eventAggregator.GetEvent<AllVehiclesEvent>().Publish(Vehicles);
               _eventAggregator.GetEvent<AllTaxesEvent>().Publish(Taxes);

               if (_user != null && _user.Status)
               {
                   _user.LastLogin = DateTime.Now;
                   _user.LoginStatus = true;
                   _unitOfWork.UserRepository.Update(_user);
                   await _unitOfWork.SaveAsync();
                   _regionManager.RequestNavigate(RegionNames.MainContentRegion, "Home");
                   _regionManager.RequestNavigate(RegionNames.ShellRegion, "MainWindow");
               }
               else
               {
                   _regionManager.RequestNavigate(RegionNames.ShellRegion, "LoginWindow");
               }
           }, null, TaskScheduler.FromCurrentSynchronizationContext());                
        }
    }

    private async  Task<Models.User> CheckUser()
    {
        IsoStorage.Instance.TryGetValue("UserId", out _userId);
        _user = await _unitOfWork.UserRepository.GetAsync((int)_userId);
        return _user;
    }

    private async Task Preloading()
    {
        Customers = await _unitOfWork.CustomerRepository.GetAsync();
        Invoices = await _unitOfWork.InvoiceRepository.GetAsync();
        Users = await _unitOfWork.UserRepository.GetAsync();
        Vehicles = await _unitOfWork.VehicleRepository.GetAsync();
        Taxes = await _unitOfWork.TaxRepository.GetAsync();
    }
}

这是示例订阅者

public AdminProfileViewModel(IEventAggregator eventAggregator)
    {
        Users = new List<Models.User>();
        eventAggregator.GetEvent<AllUsersEvent>().Subscribe(AdminAllUsers, ThreadOption.PublisherThread);
    }

    private void AdminAllUsers(List<Models.User> obj)
    {
        Users = obj;
    }

0 个答案:

没有答案