【问题标题】:How to change MapType of Map in VM?如何在虚拟机中更改 Map 的 MapType?
【发布时间】:2021-06-13 04:18:42
【问题描述】:

我有一张想要从 VM 添加的地图。当您单击按钮时,我尝试添加命令 MapType 将被更改。但它不起作用。我知道如何在代码隐藏中制作它。但我需要 VM

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

虚拟机

        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;

        }

当我点击按钮时地图不会改变 MapType.pls 帮助

【问题讨论】:

  • 为什么你的虚拟机包含一个 Map 对象?
  • 我想制作一个地图和命令,比如改变地图的类型或添加图钉,如果你点击图钉,内容视图的外观,我想在VM中完成这一切,所以我决定添加虚拟机,我在那里看到了示例stackoverflow.com/questions/28098020/…
  • 请告诉我应用程序的性能是否存在差异,例如,如果我在代码隐藏或 VM 中执行所有操作
  • 这是一个可怕的建议。 Map 是一个 UI 控件,属于 View,而不是 VM。您可以将 Maps 属性和命令绑定到 VM,但 Map 本身不应在 VM 中。
  • 请告诉我应用程序的性能是否存在差异,例如,如果我在代码隐藏或 VM 中执行所有操作

标签: c# xamarin xamarin.forms


【解决方案1】:

我有一张想要从 VM 添加的地图。当您单击按钮时,我尝试添加命令 MapType 将被更改。但它不起作用。我知道如何在代码隐藏中制作它。但我需要 VM

如果你想改变viewmodel中的Map.Maptype,你可以看看下面的代码。

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

【讨论】:

  • 谢谢,但由于某种原因它仍然无法正常工作。显示地图和图钉,但命令不会更改地图类型
  • @Mykola2012 通过按钮命令更改maptype没有问题。你可以在github上提供一个样本,我会下载你的样本进行测试。
猜你喜欢
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
相关资源
最近更新 更多