示例#1
0
        public RoutesNewViewModel()
        {
            stopService = (App.Current as App).Container.GetService <IStopService>();
            mapService  = (App.Current as App).Container.GetService <IMapService>();

            Stops.CollectionChanged += Stops_CollectionChanged;

            Center = new Geopoint(defaultPosition);

            MapLoaded = new RelayCommand <object>((param) =>
            {
                MapControl newMap = param as MapControl;
                Map = newMap;
            });

            NewStopCommand    = new RelayCommand(async() => { await new SearchAddressDialog().ShowAsync(); });
            RemoveStopCommand = new RelayCommand <object>((param) =>
            {
                // will need to find out what's wrong with this binding at some point
                object stopModel = (param as Grid).DataContext as object;
                stopService.RemoveStop(stopModel);
            });

            SaveRouteCommand = new RelayCommand(async() => { await new NewRouteConfirmation().ShowAsync(); });

            SwitchStopUpCommand = new RelayCommand(() =>
            {
                // We are running a switch command
                isSwitchCommandRunning = true;

                // Find index of selected item in stops
                var indexOfStop = stopService.RetrieveIndexOfStop(SelectedStop as object);

                // if index is 0, do nothing
                if (indexOfStop == 0)
                {
                    return;
                }

                // Temp model object
                var tempStopToReinsert = SelectedStop.Clone();

                // Remove item from collection and reinsert at desired index (-1)
                stopService.RemoveStop(SelectedStop);

                // SelectedStop becomes null at this point, so let's use the
                // copy we made to reinsert
                stopService.AddStopAtIndex(tempStopToReinsert, indexOfStop - 1);

                // We've reached the end of the switch command
                isSwitchCommandRunning = false;
            });

            SwitchStopDownCommand = new RelayCommand(() =>
            {
                // We are running a switch command
                isSwitchCommandRunning = true;

                // Find index of selected item in stops
                var indexOfStop = stopService.RetrieveIndexOfStop(SelectedStop as object);

                // if index is the last index, do nothing
                if (indexOfStop == stopService.Stops.Count - 1)
                {
                    return;
                }

                // Temp model object
                var tempStopToReinsert = SelectedStop.Clone();

                // Remove item from collection and reinsert at desired index (+1)
                stopService.RemoveStop(SelectedStop);

                // SelectedStop becomes null at this point, so let's use the
                // copy we made to reinsert
                stopService.AddStopAtIndex(tempStopToReinsert, indexOfStop + 1);

                // We've reached the end of the switch command
                isSwitchCommandRunning = false;
            });
        }