如何在Windows Phone 8应用程序开发中杀死页面

时间:2015-06-16 11:41:14

标签: c# visual-studio-2013 windows-phone-8.1

我是Windows手机应用程序开发的新手,我来自Android背景。所以我有这个应用程序,我显示一些地方的列表,并点击任何地方,用户将显示从他/她的当前位置到该地点的位置的路线。我能够做到这一点。但是,在返回并单击其他位置时,它会同时显示前一个位置的上一个路径和当前路径。

地图页面的代码是

 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();
        }

    }
}

这就是我如何获取上一个请求和当前请求的路径。

enter image description here

我在android中寻找像Activity.finish()这样的东西,我知道这两种技术都非常不同。

3 个答案:

答案 0 :(得分:1)

您可能想查看back stack。 如果您按后退按钮,这是应用程序将浏览的页面堆栈。

您可以通过RootFrame对象访问它。这有一个BackStack属性。

你可以像这样删除最后一个;

RootFrame.RemoveBackEntry();

或通过循环将它们全部删除(除了最后一个)。

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

有了这个,您可以根据需要操纵后台,并使用后退按钮导航到右侧页面。

答案 1 :(得分:1)

除了杰拉德提到的正确的后台使用外,您还希望将latitudelongitude作为参数传递给GetRouteAndDirections函数,这样您的代码就会显示出来像:

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
    }
}

试试这个,看看它是否能解决您的问题。

答案 2 :(得分:0)

如果您只想删除地图上绘制的上一个路线,请尝试使用Clear()或Remove()方法。未经测试的代码如下:

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