Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
How to change MapType of Map in VM?

I have a map which want to add from VM. I tried to add command when you click on button MapType will be changed.but it does not working.I know how to make it in codebehind.but I need VM

ContentView
            AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
            AbsoluteLayout.LayoutFlags="All"
            Content="{Binding Map}" />

VM

        public Map Map { get; set; } 
        public Command StandartMapCommand { get; set; }

        public Command SatelliteMapCommand { get; set; }

        public Command HybridMapCommand { get; set; }
      public MasterPageVM()
        {
             Map = new Map();
           StandartMapCommand = new Command(StandardSelected);
            SatelliteMapCommand = new Command(SatelliteSelected);
            HybridMapCommand = new Command(HybridSelected);
       }
   public void StandardSelected()
        {
         
                Map.MapType = Xamarin.Forms.Maps.MapType.Street;

           
        }

        public void HybridSelected()
        {
        
                Map.MapType = Xamarin.Forms.Maps.MapType.Hybrid;
            }

        

        public void SatelliteSelected()
        {
         
                Map.MapType = Xamarin.Forms.Maps.MapType.Satellite;

        }

When I click on button map does not change MapType.pls help

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I have a map which want to add from VM. I tried to add command when you click on button MapType will be changed.but it does not working.I know how to make it in codebehind.but I need VM

If you want to change Map.Maptype in viewmodel, you can take a look the following code.

<ContentPage.Content>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ContentView Grid.ColumnSpan="3" Content="{Binding Map}" />
        <Button
            x:Name="btn1"
            Grid.Row="1"
            Command="{Binding StandartMapCommand}"
            Text="Street" />
        <Button
            x:Name="btn2"
            Grid.Row="1"
            Grid.Column="1"
            Command="{Binding SatelliteMapCommand}"
            Text="Satellite" />
        <Button
            x:Name="btn3"
            Grid.Row="1"
            Grid.Column="2"
            Command="{Binding HybridMapCommand}"
            Text="Hybrid" />

    </Grid>


</ContentPage.Content>

public partial class Page1 : ContentPage
{
    public Page1 ()
    {
        InitializeComponent ();         
        this.BindingContext = new MapViewModel();
    }
}
public class MapViewModel
{
    private Map _map;
    public Map Map
    {
        get { return _map; }
        set
        {
            _map = value;
                    
        }
    }
    public Command StandartMapCommand { get; set; }
    public Command SatelliteMapCommand { get; set; }
    public Command HybridMapCommand { get; set; }
    public MapViewModel()
    {
        Map = new Map();
        Position position = new Position(36.9628066, -122.0194722);
        MapSpan mapSpan = new MapSpan(position, 0.01, 0.01);
        Map.MoveToRegion(mapSpan);
        Map.Pins.Add(new Pin
        {
            Label = "Xamarin",
            Position = position
        });

        StandartMapCommand = new Command(streetcommand);
        SatelliteMapCommand = new Command(Satellitecommand);
        HybridMapCommand = new Command(Hybridcommand);
      
    }

    private void streetcommand()
    {
        Map.MapType = MapType.Street;
    }
    private void Satellitecommand()
    {
        Map.MapType = MapType.Satellite;
    }
    private void Hybridcommand()
    {
        Map.MapType = MapType.Hybrid;
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda