コード例 #1
0
		public OrdersListViewModel()
		{
			_ordersRepository = new OrdersWebRepository ();
			_connectivity = CrossConnectivity.Current;
			Orders = new ReactiveList<OrderViewModel>();

			// Initial connectivity availability
			CanLoadOrders = _connectivity.IsConnected;

			// Convert a .NET event to an observable 
			// and subscribe to an observer *anonymous delegate extension*.
			IObservable<EventPattern<ConnectivityChangedEventArgs>> connectivityChangedObservable = 
				Observable.FromEventPattern<ConnectivityChangedEventArgs>(
					_connectivity, 
					"ConnectivityChanged",
					RxApp.MainThreadScheduler);

			// When the IConnectivity.ConnectivityChanged event is raised
			// the observable will push me the ConnectivityChangedEventArgs.
			_connectivityChangedDisposable = connectivityChangedObservable.Subscribe(evt => {
				// Set if we can load orders
				CanLoadOrders = evt.EventArgs.IsConnected;
			});

			// Cool stuff! ReactiveUI offers some Rx helpers.
			// When the CanLoadOrders property changes let me know.
			IObservable<bool> canLoadOrdersObservable = 
				this.WhenAny(x => x.CanLoadOrders, x => x.Value);

			// More Cool stuff! ReactiveCommands have built-in support for background
			// operations. RxCmd guarantees that this block will only run exactly
			// once at a time, and that the CanExecute will auto-disable while it
			// is running.
			LoadOrdersCommand = ReactiveCommand.CreateAsyncTask(
				canLoadOrdersObservable,
				async _ => 
				{
					return await _ordersRepository.GetAsync();;
				});

			// And if that is not Cool stuff! ReactiveCommands are themselves IObservables, whose value
			// are the results from the async method, guaranteed to arrive on the UI
			// thread. We're going to take the list of teams that the background
			// operation loaded, and put them into our TeamList.
			_loadOrdersCommandDisposable = LoadOrdersCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(
				orders => {
					int currentCount = count;
					count++;

					IEnumerable<OrderViewModel> ordersTranformed = orders.Select(o => {
						o.OrderNumber = string.Format("{0} - {1}", o.OrderNumber, currentCount);
						return o;
					});

					Orders.InsertRange(0, ordersTranformed);
				},
				ex => {
					UserError.Throw("Fetching orders exception: " + ex.Message, ex);
				});

			// Niiiiice! Whenever the CanLoadOrders changes, we're going to wait
			// for one second of "dead airtime", then invoke the LoadOrdersCommand
			// command.
			_canLoadOrdersDisposable = canLoadOrdersObservable
				.Where(x => x)
				.Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
				.InvokeCommand(this, x => x.LoadOrdersCommand);
		}
コード例 #2
0
        public OrdersListViewModel()
        {
            _ordersRepository = new OrdersWebRepository();
            _connectivity     = CrossConnectivity.Current;
            Orders            = new ReactiveList <OrderViewModel>();

            // Initial connectivity availability
            CanLoadOrders = _connectivity.IsConnected;

            // Convert a .NET event to an observable
            // and subscribe to an observer *anonymous delegate extension*.
            IObservable <EventPattern <ConnectivityChangedEventArgs> > connectivityChangedObservable =
                Observable.FromEventPattern <ConnectivityChangedEventArgs>(
                    _connectivity,
                    "ConnectivityChanged",
                    RxApp.MainThreadScheduler);

            // When the IConnectivity.ConnectivityChanged event is raised
            // the observable will push me the ConnectivityChangedEventArgs.
            _connectivityChangedDisposable = connectivityChangedObservable.Subscribe(evt => {
                // Set if we can load orders
                CanLoadOrders = evt.EventArgs.IsConnected;
            });

            // Cool stuff! ReactiveUI offers some Rx helpers.
            // When the CanLoadOrders property changes let me know.
            IObservable <bool> canLoadOrdersObservable =
                this.WhenAny(x => x.CanLoadOrders, x => x.Value);

            // More Cool stuff! ReactiveCommands have built-in support for background
            // operations. RxCmd guarantees that this block will only run exactly
            // once at a time, and that the CanExecute will auto-disable while it
            // is running.
            LoadOrdersCommand = ReactiveCommand.CreateAsyncTask(
                canLoadOrdersObservable,
                async _ =>
            {
                return(await _ordersRepository.GetAsync());;
            });

            // And if that is not Cool stuff! ReactiveCommands are themselves IObservables, whose value
            // are the results from the async method, guaranteed to arrive on the UI
            // thread. We're going to take the list of teams that the background
            // operation loaded, and put them into our TeamList.
            _loadOrdersCommandDisposable = LoadOrdersCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(
                orders => {
                int currentCount = count;
                count++;

                IEnumerable <OrderViewModel> ordersTranformed = orders.Select(o => {
                    o.OrderNumber = string.Format("{0} - {1}", o.OrderNumber, currentCount);
                    return(o);
                });

                Orders.InsertRange(0, ordersTranformed);
            },
                ex => {
                UserError.Throw("Fetching orders exception: " + ex.Message, ex);
            });

            // Niiiiice! Whenever the CanLoadOrders changes, we're going to wait
            // for one second of "dead airtime", then invoke the LoadOrdersCommand
            // command.
            _canLoadOrdersDisposable = canLoadOrdersObservable
                                       .Where(x => x)
                                       .Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
                                       .InvokeCommand(this, x => x.LoadOrdersCommand);
        }