2017-03-10 2 views
0

Ich versuche, eine Xamarin Forms Map zu manipulieren, geschrieben in XAML. Ich möchte den Kartentyp bei Klick ändern, mit der Befehlseigenschaft auf meinem Button-Objekt. Mein Code hinter ist in einer separaten CS-Datei namens MapViewModel.Xamarin Forms Karten, in Code hinter durch MVVM seperate CS-Datei referenziert

Hier ist meine Ansicht in XAML

<Grid.BindingContext> 
    <mvm:MapViewModel/> 
</Grid.BindingContext> 
<Grid.RowDefinitions> 

<Button 
    HorizontalOptions="Start" 
    Text="Map Types" 
    Command="{Binding MapTypeChange}" 
    CommandParameter="{x:Reference Name=map}" 
    BackgroundColor="Olive"/> 

<Button 
    HorizontalOptions="Center" 
    Text="Report NoShows" 
    Command="{Binding SendLocationCommand}" 
    CommandParameter="{x:Reference Name=MapView}" 
    BackgroundColor="Red" 
    /> 

<Button 
    Text="Sync" 
    Command="{Binding SyncLocationCommand}" 
    CommandParameter="{x:Reference Name=MapView}" 
    BackgroundColor="Green" 
    /> 


<StackLayout 
    Padding="5,50,5,0"> 
    <maps:Map 
     Grid.Column="1" 
     Grid.Row="0" 
     WidthRequest="350" 
     HeightRequest="500" 
     x:Name="XamlMap" 
     IsShowingUser="true" 
     MapType="Hybrid" /> 
</StackLayout> 
</Grid> 

Und hier ist mein MapViewModel

public class MapViewModel : INotifyPropertyChanged 
{ 
    private ActionCommand _sendlocationcommand; 
    public ActionCommand SendLocationCommand 
    { 
     get 
     { 
      if (_sendlocationcommand == null) 
      { 
       _sendlocationcommand = new ActionCommand(SendEmailLocation); 
      } 
      return _sendlocationcommand; 
     } 
    } 

    private ActionCommand<Page> _synclocationcommand; 
    public ActionCommand<Page> SyncLocationCommand 
    { 
     get 
     { 
      if (_synclocationcommand == null) 
      { 
       _synclocationcommand = new ActionCommand<Page>(SyncLocation); 
      } 
      return _synclocationcommand; 
     } 
    } 

    private ActionCommand<Map> _maptypecommand; 
    public event PropertyChangedEventHandler PropertyChanged; 

    public ActionCommand<Map> MapTypeCommand 
    { 
     get 
     { 
      if (_maptypecommand == null) 
      { 
       _maptypecommand = new ActionCommand<Map>(MapTypeChange); 
       // OnPropertyChanged("MapTypeCommand"); 

      } 
      return _maptypecommand; 
     } 
    } 


    //**Here is where I want to manipulate the map** 
    private async void MapTypeChange(Map map) 
    { 
     if (map != null) 
      map.MapType = MapType.Satellite; 
     var locator = CrossGeolocator.Current; 
     var currentPos = await locator.GetPositionAsync(timeoutMilliseconds: 10000); 

     map.MoveToRegion(MapSpan.FromCenterAndRadius(
      new Xamarin.Forms.Maps.Position(currentPos.Latitude, currentPos.Longitude), Distance.FromMiles(1))); 
    } 

    private async void SyncLocation(Page page) 
    { 
     var locator = CrossGeolocator.Current; 
     if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled) 
     { 
      locator.DesiredAccuracy = 50; 
      var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000); 
      string currentposition = ($"{position.Timestamp.UtcDateTime}"); 
      string latitude = ($"Latitude: {position.Latitude}"); 
      string longitutde = ($" {position.Longitude}"); 
      string latlngposition = ($"{latitude}, {longitutde}"); 
      string cancel = "Cancel"; 
      await page.DisplayAlert($"Synchronizing .. Please wait... :{currentposition}", $"{latlngposition}", $"{cancel}"); 
      double lat = position.Latitude; 
      double lng = position.Longitude; 
     } 
     else if (!locator.IsGeolocationEnabled) 
     { 
      await page.DisplayAlert("Device Location Error :", "Is location on ? Location is off or unavaliable please check this, and restart the application", "Exit"); 
      throw new ArgumentException("Device location not found, or unavalible,"); 
     } 

    } 

    private async void SendEmailLocation() 
    { 
     var locator = CrossGeolocator.Current; 
     if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled) 
     { 
      var emailmessagebuilder = CrossMessaging.Current.EmailMessenger; 
      locator.DesiredAccuracy = 50; 
      var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000); 
      string currentposition = ($"Date & Time : {position.Timestamp.UtcDateTime}"); 
      string latitude = ($"Latitude: {position.Latitude}"); 
      string longitutde = ($"Longitude: {position.Longitude}"); 
      string latlngposition = ($"Latitude :{latitude}, Longitutde :{longitutde}, {currentposition}"); 

      if (emailmessagebuilder.CanSendEmail) 
      { 
       var email = new EmailMessageBuilder() 
        .To("") 
        .Cc("") 
        .Bcc(new[] { "", "" }) 
        .Subject("") 
        .Body($"{latlngposition}") 
        .Build(); 
       emailmessagebuilder.SendEmail(email); 
      } 
     } 
     else if (!locator.IsGeolocationEnabled) 
     { 
      var page = new Page(); 
      await page.DisplayAlert("Device Location Service Error!", "Is location on ? Location is off or unavaliable please try again", "Exit"); 
      throw new ArgumentException("Device location not found, or unavalible,"); 
     } 
    } 

    public void OnPropertyChanged(string prop) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop)); 
    } 
} 
+0

Danke cssharpbd :) – Ahhzeee

Antwort

0

ich es herausgefunden.

1.Give XAML-Objekt ein X: Name = "XamlMap"

  1. In der XAML-Datei Führen Sie die Taste, um ein
    Command Objekt = "{x: Referenz Name = XamlMap}“

  2. Ihre Cs die Methode Datei, die das Objekt ist der Umgang, übergeben Sie es Ihr Objekt als Parameter wie diese

    private void MapTypeChange(Map map) 
    { 
    
        var xamlMap = map.FindByName<Map>("XamlMap"); 
        xamlMap.MapType = MapType.Satellite; 
    
    } 
    

übergeben wir unserer Methode eine Referenz auf ein Objekt desselben Typs.

verwenden wir eine Variable, und verwenden Sie die FindByName unserer generischen Typ vorbei, und wir

unsere FindByName einen String-Parameter unserer XAML Objektnamen übergeben.

dann verwenden wir die Variable, um zu tun, was überhaupt wollen !!!

Glückliche Codierung!

Verwandte Themen