public MenuViewModel(IUserRepository userRepository)
 {
     if (userRepository == null) 
         throw new ArgumentNullException("userRepository");
     _userRepository = userRepository;
     Menu = new ReactiveList<MenuOptionViewModel>();
     // Use WhenAny to observe one or more values
     var canLoadMenu = this.WhenAny(m => m.User, user => user.Value != null);
     // hook function to command, shouldn't contain UI/complex logic
     LoadMenu = ReactiveCommand.CreateAsyncTask(canLoadMenu, _ => _userRepository.GetMenuByUser(User));
     // RxApp.MainThreadScheduler is our UI thread, you can go wild here
     LoadMenu.ObserveOn(RxApp.MainThreadScheduler).Subscribe(menu =>
     {
         Menu.Clear();
         foreach (var option in menu)
         {
             var menuOption = new MenuOptionViewModel(option);
             Menu.Add(menuOption);
         }
     });
     LoadMenu.ThrownExceptions.Subscribe(ex =>
     {
         Menu.Clear();
         MessageBox.Show(ex.Message);
     });
     // Use WhenAnyValue to check if a property was changed
     // If user was changed reload menu
     this.WhenAnyValue(m => m.User).InvokeCommand(this, vm => vm.LoadMenu);
 }
示例#2
0
        public MenuViewModel(IUserRepository userRepository)
        {
            if (userRepository == null)
            {
                throw new ArgumentNullException("userRepository");
            }
            _userRepository = userRepository;
            Menu            = new ReactiveList <MenuOptionViewModel>();
            // Use WhenAny to observe one or more values
            var canLoadMenu = this.WhenAny(m => m.User, user => user.Value != null);

            // hook function to command, shouldn't contain UI/complex logic
            LoadMenu = ReactiveCommand.CreateFromTask <User, IList <Menu> >(async user => await _userRepository.GetMenuByUser(User),
                                                                            canLoadMenu);
            // RxApp.MainThreadScheduler is our UI thread, you can go wild here
            LoadMenu.ObserveOn(RxApp.MainThreadScheduler).Subscribe(menu =>
            {
                Menu.Clear();
                foreach (var option in menu)
                {
                    var menuOption = new MenuOptionViewModel(option);
                    Menu.Add(menuOption);
                }
            });
            LoadMenu.ThrownExceptions.Subscribe(ex =>
            {
                Menu.Clear();
                MessageBox.Show(ex.Message);
            });
            // Use WhenAnyValue to check if a property was changed
            // If user was changed reload menu
            this.WhenAnyValue(m => m.User).InvokeCommand(this, vm => vm.LoadMenu);
        }