Pranjal
Pranjal

Reputation: 27

How to kill a page in Windows Phone 8 app development

I am new to windows phone app development and I am coming from an android background. So I have this app where I display the list of some places and on clicking on any place the user is to be shown the route from his/her current location to the location of the place. I am able to achieve this. However on going back and clicking another place, it shows both the the previous route to previous location and the current path too.

The code for the Map Page is

 namespace MapApper
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private double latitude, longitude;
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }
        private async void GetRouteAndDirections()
        {
            Geolocator geolocator = new Geolocator();
            Geoposition geoposition = null;

            geoposition = await geolocator.GetGeopositionAsync();

            // Start at Microsoft in Redmond, Washington.
            BasicGeoposition startLocation = new BasicGeoposition();
            startLocation.Latitude = geoposition.Coordinate.Latitude;
            startLocation.Longitude = geoposition.Coordinate.Longitude;
            Geopoint startPoint = new Geopoint(startLocation);

            // End at the city of Seattle, Washington.
            BasicGeoposition endLocation = new BasicGeoposition();
            endLocation.Latitude = latitude;
            endLocation.Longitude = longitude;
            Geopoint endPoint = new Geopoint(endLocation);

            // Get the route between the points.
            MapRouteFinderResult routeResult =
                await MapRouteFinder.GetDrivingRouteAsync(
                startPoint,
                endPoint,
                MapRouteOptimization.Time,
                MapRouteRestrictions.None);

            textblock.Text = routeResult.Status.ToString()+latitude+longitude;
            if (routeResult.Status == MapRouteFinderStatus.Success)
            {
                // Use the route to initialize a MapRouteView.
                MapRouteView viewOfRoute = new MapRouteView(routeResult.Route);
                viewOfRoute.RouteColor = Colors.Yellow;
                viewOfRoute.OutlineColor = Colors.Black;

                // Add the new MapRouteView to the Routes collection
                // of the MapControl.
                mapper.Routes.Add(viewOfRoute);

                // Fit the MapControl to the route.
                await mapper.TrySetViewBoundsAsync(
                    routeResult.Route.BoundingBox,
                    null,
                    Windows.UI.Xaml.Controls.Maps.MapAnimationKind.None);
            }
        }




        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.
            textblock.Text = "EnteringOn";
            show s = (show)e.Parameter;
            latitude = s.latitude;
            longitude = s.longitude;
            GetRouteAndDirections();
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }
       private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            e.Handled = true;
            if (Frame.CanGoBack)
                Frame.GoBack();
        }

    }
}

This is how I am getting the path for previous request and the current request together.

enter image description here

I am looking for something like Activity.finish() in android, I know both the technology are very different.

Upvotes: 1

Views: 209

Answers (3)

shrpr
shrpr

Reputation: 1

If you are just looking to remove the previous route drawn on the map, try using the Clear() or Remove() method. Untested code below:

mapper.Routes.Clear();
mapper.Routes.Add(viewOfRoute);

Upvotes: 0

Umair
Umair

Reputation: 5481

Apart from the correct back-stack usage as Gerald has mentioned, you also would want to pass the latitude and longitude to the GetRouteAndDirections function as parameters and so your code would look like:

namespace MapApper
{
    public sealed partial class MainPage : Page
    {
        // private double latitude, longitude; // comment this
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }
        private async void GetRouteAndDirections(double latitude, double longitude)
        {

            //... your code

            endLocation.Latitude = latitude;
            endLocation.Longitude = longitude;

            //... your code

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //... your code

            show s = (show)e.Parameter;
            // latitude = s.latitude; // comment this
            // longitude = s.longitude; // comment this
            GetRouteAndDirections((double)s.latitude, (double)s.longitude); // you might want to cast the lat and lon as double

            //... your code

        }

        //... your code
    }
}

Try this and see if it would fix your issue.

Upvotes: 1

Gerald Versluis
Gerald Versluis

Reputation: 34093

You probably want to look at the back stack. This is the stack of pages that the app will navigate through if you press the back button.

You can access it through the RootFrame object. This has a BackStack property.

You can erase the last one like this;

RootFrame.RemoveBackEntry();

or remove them all (except last) by looping through it.

for (int i = 0; i < historyListBox.SelectedIndex; i++)
{
   RootFrame.RemoveBackEntry();
}

With this you can manipulate the back stack to your needs and navigate to the right page with the back button.

Upvotes: 1

Related Questions