Пример #1
0
        private readonly EventHandler <CancelEventArgs> _backButtonHandler;  // Hold onto the delegate so we can unsubscribe during navigation away from the page

        /// <summary>Constructor, retrieves a refernce to our ViewModel which has been set via the ViewModelLocator in XAML</summary>
        public MainView()
        {
            try
            {
                InitializeComponent();

                // Get a reference to our view model, which has been set in XAML
                ViewModel = this.DataContext as IMainViewModel;
                if (ViewModel == null)
                {
                    throw new NullReferenceException();
                }

                // Hook into the ViewModel's MapRouteChanged event. This allows us to display the
                // route (if any) to the RoundUp location point
                ViewModel.MapRouteChanged += (sender, args) =>
                {
                    try
                    {
                        // Remove any previous route
                        if (_mapRouteToRoundUpLocation != null)
                        {
                            LocationMap.RemoveRoute(_mapRouteToRoundUpLocation);
                        }

                        // Save the route...
                        _mapRouteToRoundUpLocation = args.NewRoute;

                        // The ViewModel can remove the existing route by raising the MapRouteChanged event and supplying a null NewRoute
                        if (args.NewRoute == null)
                        {
                            return;
                        }

                        // ... and add it to the map
                        LocationMap.AddRoute(_mapRouteToRoundUpLocation);
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex, "Error modifying route. ", new StackFrame(0, true));
                    }
                };

                // Hook into the ViewModel's ShareUiVisibilityChanged event. This allows us to start/stop the "sonar" annimation
                _sonarTimer = new Timer(SonarWaitOnCompleted, null, -1, -1);
                ViewModel.ShareUiVisibilityChanged += (sender, args) =>
                {
                    if (!ViewModel.ShowShareUI)
                    {
                        // Close the share panel and stop the sonar animation
                        _sonarTimer.Change(-1, -1);  // Stop the timer
                        if (_subscribedToStoryboardEvents)
                        {
                            StoryboardSonar.Completed -= StoryboardSonarOnCompleted;
                        }
                        StoryboardSonar.Stop();              // Stop the sonar animation
                        ShareUiPanelCloseStoryboard.Begin(); // Close the share panel
                        return;
                    }

                    // Open the share panel and start the sonar animation
                    StoryboardSonar.Completed    += StoryboardSonarOnCompleted;
                    _subscribedToStoryboardEvents = true;
                    StoryboardSonar.Begin();            // Start the sonar animation
                    ShareUiPanelOpenStoryboard.Begin(); // Open the share panel
                };

                // Hook into the CurrentLocationChanged event so we can update the position of the current location "sonar" animation
                ViewModel.CurrentLocationChanged += (sender, args) =>
                {
                    var currentLoc = LocationMap.ConvertGeoCoordinateToViewportPoint(args.Location);
                    SonarCurrentLocation.Margin = new Thickness(
                        currentLoc.X - SonarCurrentLocation.Width / 2,
                        currentLoc.Y - SonarCurrentLocation.Height / 2,
                        0,
                        0);

                    SonarCurrentLocation.Visibility = Visibility.Visible;
                    Deployment.Current.Dispatcher.BeginInvoke(() => StoryboardCurrentLocation.Begin());
                };
                StoryboardCurrentLocation.Completed += (sender, args) => { SonarCurrentLocation.Visibility = Visibility.Collapsed; };

                // Hook into the InviteeSelected event so we can place a "sonar" animation around the currently selected invitee
                ViewModel.InviteeSelected += (sender, args) =>
                {
                    var inviteeLoc = LocationMap.ConvertGeoCoordinateToViewportPoint(args.Location);
                    SonarSelectedInvitee.Margin = new Thickness(
                        inviteeLoc.X - SonarSelectedInvitee.Width / 2,
                        inviteeLoc.Y - SonarSelectedInvitee.Height / 2,
                        0,
                        0);

                    SonarSelectedInvitee.Visibility = Visibility.Visible;
                    Deployment.Current.Dispatcher.BeginInvoke(() => StoryboardSelectedInvitee.Begin());
                };
                StoryboardSelectedInvitee.Completed += (sender, args) => { SonarSelectedInvitee.Visibility = Visibility.Collapsed; };

                // Pass on the backKeyPress event to the view model
                // 9/4/14 changed from using anonymous delegate so we can unsubscribe from the event during OnNavigatedFrom as recommended by MS
                //this.BackKeyPress += (sender, args) => ViewModel.BackKeyPress(args);
                _backButtonHandler = (sender, args) => ViewModel.BackKeyPress(args);
                this.BackKeyPress += _backButtonHandler;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, "Fatal exception in MainView ctor. ", new StackFrame(0, true));
            }
        }