Ejemplo n.º 1
0
        /// <summary>
        /// Handles the BindingContextChanged event.
        /// </summary>
        protected override void OnBindingContextChanged()
        {
            // Remove the event handling from the binding context
            if (this.currentBindingContext != null)
            {
                this.pinsSynchronizer.Dispose();
                this.pinsSynchronizer = null;
                this.currentBindingContext.Navigation = null;

                // Remove events handler
                this.MapView.Pins.Clear();
                this.currentBindingContext.PropertyChanged -= this.OnPropertyChanged;
                this.currentBindingContext.ErrorReported   -= this.OnErrorReported;
            }

            // Set up the event handling from the binding context
            this.currentBindingContext = this.BindingContext as MainViewModel;
            if (this.currentBindingContext != null)
            {
                // Set up navigation context
                this.currentBindingContext.Navigation = this.Navigation;

                // Set up event handlers
                this.pinsSynchronizer = new CollectionSynchronizer <MapExPin>(
                    this.currentBindingContext.Pins,
                    this.MapView.Pins);
                this.currentBindingContext.PropertyChanged += this.OnPropertyChanged;
                this.currentBindingContext.ErrorReported   += this.OnErrorReported;

                // The position is not the default one
                if (this.currentBindingContext.UserPosition != default(Position))
                {
                    // Move to the selected position
                    this.OnPropertyChanged(
                        this.currentBindingContext,
                        new PropertyChangedEventArgs(nameof(MainViewModel.UserPosition)));
                }
            }

            // Call the base member
            base.OnBindingContextChanged();
        }