C# 如何在Windows Phone 8应用程序开发中删除页面

C# 如何在Windows Phone 8应用程序开发中删除页面,c#,visual-studio-2013,windows-phone-8.1,C#,Visual Studio 2013,Windows Phone 8.1,我是windows phone应用程序开发的新手,来自android背景。因此,我有一个应用程序,其中我显示了一些地方的列表,点击任何一个地方,就会向用户显示从他/她当前位置到该地方位置的路线。我能够做到这一点。但是,返回并单击另一个位置时,它会同时显示到上一位置的上一条路线和当前路径 地图页面的代码是 namespace MapApper { /// <summary> /// An empty page that can be used on its own o

我是windows phone应用程序开发的新手,来自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();
        }

    }
}
名称空间映射器
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
私人双纬度、经度;
公共主页()
{
this.InitializeComponent();
this.NavigationCacheMode=NavigationCacheMode.Required;
}
私有异步void GetRouteAndDirections()
{
Geolocator Geolocator=新的Geolocator();
地理位置地理位置=空;
geoposition=等待geolocator.GetGeopositionAsync();
//从华盛顿雷德蒙的微软开始。
BasicGeoposition startLocation=新的BasicGeoposition();
纬度=地理位置坐标纬度;
startLocation.Longitude=geoposition.Coordinate.Longitude;
地质点起始点=新的地质点(位置);
//在华盛顿州西雅图市结束。
BasicGeoposition endLocation=新的BasicGeoposition();
结束位置。纬度=纬度;
经度=经度;
地质点端点=新的地质点(端点位置);
//找到两点之间的路线。
MapRouteFinderResult路由结果=
等待MapRouteFinder.GetDrivingRouteAsync(
起点,
终点,
MapRouteOptimization.时间,
地图路线限制。无);
textblock.Text=routeResult.Status.ToString()+纬度+经度;
if(routeResult.Status==MapRouteFinderStatus.Success)
{
//使用路由初始化MapRouteView。
MapRouteView viewOfRoute=新的MapRouteView(routeResult.Route);
viewOfRoute.RouteColor=Colors.Yellow;
viewOfRoute.OutlineColor=Colors.Black;
//将新的MapRouteView添加到Routes集合
//地图控件的。
mapper.Routes.Add(viewOfRoute);
//将地图控件适配到管线。
等待mapper.TrySetViewBoundsAsync(
routeResult.Route.BoundingBox,
无效的
Windows.UI.Xaml.Controls.Maps.MapAnimationKind.None);
}
}
/// 
///当此页面即将显示在框架中时调用。
/// 
///描述如何到达此页面的事件数据。
///此参数通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
//TODO:准备在此处显示的页面。
textblock.Text=“EnteringOn”;
show s=(show)e.参数;
纬度=南纬;
经度=南纬度;
GetRouteAndDirections();
Windows.Phone.UI.Input.HardwareButtons.BackPressed+=硬件按钮\u BackPressed;
//TODO:如果应用程序包含多个页面,请确保
//通过注册来处理硬件后退按钮
//Windows.Phone.UI.Input.HardwareButtons.BackPressed事件。
//如果您正在使用某些模板提供的NavigationHelper,
//此事件已为您处理。
}
私有无效硬件按钮(对象发送器,Windows.Phone.UI.Input.BackPressedEventArgs e)
{
e、 已处理=正确;
if(框架CanGoBack)
Frame.GoBack();
}
}
}
这就是我如何获得前一个请求和当前请求的路径


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

您可能想看看。 这是应用程序在按下后退按钮时将浏览的页面堆栈

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

你可以像这样擦除最后一个

RootFrame.RemoveBackEntry();
或者通过循环将它们全部移除(最后一个除外)

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

使用此功能,您可以根据需要操作后堆栈,并使用“后退”按钮导航到正确的页面。

除了Gerald提到的正确后堆栈用法之外,您还需要将
纬度
经度
作为参数传递给
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
    }
}

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

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

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

您可以尝试使用类似于
mapper.Routes.Clear()
的方法清除上一条路由。我已经尝试了该实现,但是我仍然能够看到上一条路由和当前路由。好像地图没有刷新数据。您是否尝试使用
mapper.Routes.Clear()
或类似工具清除上一条路由?