使用导航栏

时间:2017-07-18 16:19:03

标签: c# android ios xamarin.forms

我想通过点击工具栏按钮来拉伸我的地图。 我成功就是去了另一个页面(MapFullPage)。

问题是我希望通过App.Current.Main = new MainMasterDetailPage(new MapPage())覆盖BackButtonPressed时它会让我失望,但我的缩放不是更理想的缩放(当我离开此页面并继续使用MapFullPage时) ,缩放很小(我可以看到大陆)。 如果我拨打App.Current.Main = new MapPage(),则会显示正确的缩放。

这是我的两个班级: MapPage - >

  public class MapPage : ContentPageCustom
  {
    //TKCustomMap mapView;
    //TKCustomMapPin pinSelected;

    DealerLocationResponseModel responseLocations;
    DealerLocationResponseModel responseShops;
    DealerLocationResponseModel responseStats;
    DealerLocationResponseModel responseMoney;

    public MapPage()
    {
        try
        {
            if (IsBusy)
                return;

            IsBusy = true;

            this.CreateView();
        }
        catch (Exception ex)
        {
            DisplayAlert("Error", Constants.StandardError, "OK");
        }
        finally
        {
            IsBusy = false;
        }
    }

    private async void CreateView()
    {
        try
        {
            Icon = "map-alt.png";
            Title = "Locations";

            var mapView = new TKCustomMap();

            mapView.IsShowingUser = true;
            mapView.SetBinding(TKCustomMap.CustomPinsProperty, "Pins");
            mapView.SetBinding(TKCustomMap.MapClickedCommandProperty, "MapClickedCommand");
            mapView.SetBinding(TKCustomMap.MapLongPressCommandProperty, "MapLongPressCommand");
            mapView.SetBinding(TKCustomMap.MapCenterProperty, "MapCenter");
            mapView.SetBinding(TKCustomMap.PinSelectedCommandProperty, "PinSelectedCommand");
            mapView.SetBinding(TKCustomMap.SelectedPinProperty, "SelectedPin");
            mapView.SetBinding(TKCustomMap.RoutesProperty, "Routes");
            mapView.SetBinding(TKCustomMap.PinDragEndCommandProperty, "DragEndCommand");
            mapView.SetBinding(TKCustomMap.CirclesProperty, "Circles");
            mapView.SetBinding(TKCustomMap.CalloutClickedCommandProperty, "CalloutClickedCommand");
            mapView.SetBinding(TKCustomMap.PolylinesProperty, "Lines");
            mapView.SetBinding(TKCustomMap.PolygonsProperty, "Polygons");
            mapView.SetBinding(TKCustomMap.MapRegionProperty, "MapRegion");
            mapView.SetBinding(TKCustomMap.RouteClickedCommandProperty, "RouteClickedCommand");
            mapView.SetBinding(TKCustomMap.RouteCalculationFinishedCommandProperty, "RouteCalculationFinishedCommand");
            mapView.SetBinding(TKCustomMap.TilesUrlOptionsProperty, "TilesUrlOptions");
            mapView.SetBinding(TKCustomMap.MapFunctionsProperty, "MapFunctions");
            mapView.IsRegionChangeAnimated = true;

            NavigationPage.SetHasBackButton(this, false);
            BackgroundColor = Color.White;

            responseLocations = ServiceProvider.GetDealerLocationServiceMethod(new DealerLocationRequestModel { MobilePhoneNumber = "063333333", UserIdentificator = "1234" });
            if (!responseLocations.Succeeded)
            {
                await DisplayAlert("Greska", "Nije nadjena nijedna lokacija", "OK");
                App.Current.MainPage = new HomeMasterDetail();
                return;
            }

            List<TKCustomMapPin> pinLists = new List<TKCustomMapPin>();
            foreach (var item in responseLocations.Dealers)
            {
                pinLists.Add(

                    new TKCustomMapPin
                    {
                        Image = "_mapPin_36x36",
                        IsCalloutClickable = true,
                        ShowCallout = true,
                        Position = new Position(item.Latitude, item.Longitude),
                        Title = item.LocationName + " " + item.Address,
                        Subtitle = item.LocationDescription
                    }
                );
            }
            mapView.CustomPins = pinLists;

            #region Stack and Buttons
            mapView = new TKCustomMap(MapSpan.FromCenterAndRadius(mapView.CustomPins.ElementAt(1).Position, Distance.FromKilometers(2)));
            mapView.CustomPins = new List<TKCustomMapPin>(pinLists);
            mapView.IsShowingUser = true;

            StackLayout btnCart = ElementsHelper.createImageButton("_cart_512x512.png", "Shops");
            StackLayout btnStats = ElementsHelper.createImageButton("_stats_512x512.png", "Top up");
            StackLayout btnMoney = ElementsHelper.createImageButton("_money_512x512.png", "Cash out");

            StackLayout layout = new StackLayout
            {

                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.Center,
                Children =
            {
            btnStats,
            btnMoney,
            btnCart
            }
            };

            BoxView box1 = new BoxView { BackgroundColor = Color.Transparent, HeightRequest = Device.GetNamedSize(NamedSize.Large, typeof(Image)) * 2.5, WidthRequest = Device.GetNamedSize(NamedSize.Large, typeof(Image)) * 2.5 };
            BoxView box2 = new BoxView { BackgroundColor = Color.Transparent, HeightRequest = Device.GetNamedSize(NamedSize.Large, typeof(Image)) * 2.5, WidthRequest = Device.GetNamedSize(NamedSize.Large, typeof(Image)) * 2.5 };

            ActivityIndicator activityIndicator = new ActivityIndicator();
            activityIndicator.Color = Constants.iPink;
            activityIndicator.BindingContext = this;
            activityIndicator.IsRunning = true;
            activityIndicator.HeightRequest = 70;
            activityIndicator.Margin = new Thickness(15, 0);
            activityIndicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsBusy");
            activityIndicator.SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy");
            layout.Children.Add(activityIndicator);

            StackLayout layoutIndicator = new StackLayout { Orientation = StackOrientation.Horizontal };
            layoutIndicator.Children.Add(box1);
            layoutIndicator.Children.Add(activityIndicator);
            layoutIndicator.Children.Add(box2);
            layoutIndicator.IsVisible = false;

            TapGestureRecognizer btnShopRecognizer = new TapGestureRecognizer();
            btnShopRecognizer.Tapped += async (sender, e) =>
            {
                if (IsBusy)
                    return;

                IsBusy = true;
                layout.IsVisible = false;
                layoutIndicator.IsVisible = true;
                await Task.Delay(400);
                responseShops = ServiceProvider.GetDealerLocationServiceMethod(new DealerLocationRequestModel { MobilePhoneNumber = "063333333", UserIdentificator = "1234" }, 1);
                SetPins(mapView, responseShops);
                mapView.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(responseShops.Dealers.ElementAt(1).Latitude, responseShops.Dealers.ElementAt(1).Longitude), Distance.FromKilometers(2)));
                SetActiveButtonMap(btnCart, btnStats, btnMoney);
                IsBusy = false;
                layoutIndicator.IsVisible = false;
                layout.IsVisible = true;
            };


            TapGestureRecognizer btnMoneyRecognizer = new TapGestureRecognizer();
            btnMoneyRecognizer.Tapped += async (sender, e) =>
            {
                if (IsBusy)
                    return;

                IsBusy = true;
                layout.IsVisible = false;
                layoutIndicator.IsVisible = true;
                await Task.Delay(400);
                responseMoney = ServiceProvider.GetDealerLocationServiceMethod(new DealerLocationRequestModel { MobilePhoneNumber = "063333333", UserIdentificator = "1234" }, 2);
                SetPins(mapView, responseMoney);
                mapView.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(responseMoney.Dealers.ElementAt(1).Latitude, responseMoney.Dealers.ElementAt(1).Longitude), Distance.FromKilometers(2)));
                SetActiveButtonMap(btnMoney, btnStats, btnCart);
                IsBusy = false;
                layoutIndicator.IsVisible = false;
                layout.IsVisible = true;
            };

            TapGestureRecognizer btnStatsRecognizer = new TapGestureRecognizer();
            btnStatsRecognizer.Tapped += async (sender, e) =>
            {
                if (IsBusy)
                    return;

                IsBusy = true;
                layout.IsVisible = false;
                layoutIndicator.IsVisible = true;
                await Task.Delay(400);
                responseStats = ServiceProvider.GetDealerLocationServiceMethod(new DealerLocationRequestModel { MobilePhoneNumber = "063333333", UserIdentificator = "1234" }, 3);
                SetPins(mapView, responseStats);
                mapView.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(responseStats.Dealers.ElementAt(1).Latitude, responseStats.Dealers.ElementAt(1).Longitude), Distance.FromKilometers(2)));
                SetActiveButtonMap(btnStats, btnMoney, btnCart);
                IsBusy = false;
                layoutIndicator.IsVisible = false;
                layout.IsVisible = true;
            };


            btnCart.GestureRecognizers.Add(btnShopRecognizer);
            btnMoney.GestureRecognizers.Add(btnMoneyRecognizer);
            btnStats.GestureRecognizers.Add(btnStatsRecognizer); 
            #endregion

            RelativeLayout _baseLayout = new RelativeLayout { VerticalOptions = LayoutOptions.FillAndExpand };
            _baseLayout.Children.Add(
            mapView,
                Constraint.RelativeToParent((parent) =>
                {
                    return (0);
                }),
                Constraint.RelativeToParent((parent) =>
                {
                    return (0);
                }),
                heightConstraint: Constraint.RelativeToParent((r) => r.Height),
                widthConstraint: Constraint.RelativeToParent((r) => r.Width));

            Func<RelativeLayout, double> getLayoutWidth = (p) => layout.Measure(_baseLayout.Width, _baseLayout.Height).Request.Width;

            _baseLayout.Children.Add(layout,
            Constraint.RelativeToParent((parent) => parent.Width / 2 - getLayoutWidth(parent) / 2),
            Constraint.RelativeToParent((parent) =>
            {
                return (.8 * parent.Height);
            })
            );

            _baseLayout.Children.Add(layoutIndicator,
            Constraint.RelativeToParent((parent) => parent.Width / 2 - getLayoutWidth(parent) / 2),
            Constraint.RelativeToParent((parent) =>
            {
                return (.8 * parent.Height);
            })
            );

            //_baseLayout.Children.Add(activityIndicator,
            //Constraint.RelativeToParent((parent) => parent.Width / 2),
            //Constraint.RelativeToParent((parent) =>
            //{
            //    return (.8 * parent.Height);
            //})
            //);

            Constraint.RelativeToParent((parent) =>
            {
                return (1);
            });
            _baseLayout.HeightRequest = App.ScreenHeight - 200;

            Content = _baseLayout;
        }
        catch
        {
            await DisplayAlert("Error", Constants.StandardError, "OK");
        }
        //this._baseLayout.Children.Add(
        //    mapView,
        //    Constraint.RelativeToView(autoComplete, (r, v) => v.X),
        //    Constraint.RelativeToView(autoComplete, (r, v) => autoComplete.HeightOfSearchBar),
        //    heightConstraint: Constraint.RelativeToParent((r) => r.Height - autoComplete.HeightOfSearchBar),
        //    widthConstraint: Constraint.RelativeToView(autoComplete, (r, v) => v.Width));

        //this._baseLayout.Children.Add(
        //    autoComplete,
        //    Constraint.Constant(0),
        //    Constraint.Constant(0));


    }
    private void SetPins(TKCustomMap map, DealerLocationResponseModel dealerModel = null)
    {
        List<TKCustomMapPin> pinLists = new List<TKCustomMapPin>();
        foreach (var item in dealerModel.Dealers)
        {
            pinLists.Add(

                new TKCustomMapPin
                {
                    Image = "_mapPin_36x36",
                    IsCalloutClickable = true,
                    ShowCallout = true,
                    Position = new Position(item.Latitude, item.Longitude),
                    Title = item.LocationName + " " + item.Address,
                    Subtitle = item.LocationDescription,
                }
            );
        }
        //map.CustomPins = null;
        map.CustomPins = pinLists;

    }
    private void SetActiveButtonMap(StackLayout activeImage, StackLayout disabledImage1, StackLayout disabledImage2)
    {

        var a = ((activeImage.Children[0] as Image).Source as FileImageSource).File;
        if ((!((activeImage.Children[0] as Image).Source as FileImageSource).File.ToString().Contains("_active")))
            //Set active image
            StartClickCustom(activeImage.Children[0] as Image);

        //Remove active images

        if (((disabledImage1.Children[0] as Image).Source as FileImageSource).File.ToString().Contains("_active"))
        {
            EndClickCustom(disabledImage1.Children[0] as Image);
        }
        if (((disabledImage2.Children[0] as Image).Source as FileImageSource).File.ToString().Contains("_active"))
        {
            EndClickCustom(disabledImage2.Children[0] as Image);
        }

    }
}

和MapFullPage是相同的(复制/粘贴)只有onBack方法:

        protected override bool OnBackButtonPressed()
    {
        App.fromBack = true;
        App.Current.MainPage = new MainMasterDetailPage(new MapPage());
        return true;
    }

0 个答案:

没有答案