0

I made a CustomMap that is working if I put my code in xaml.cs but I am working whit MVVM pattern.

<ContentPage.Content>
        <local:CustomMap x:Name="customMap" MapType="Street" />
 </ContentPage.Content>

I need to acces x:Name on my ViewModel to be able to do this for example:

var pin = new CustomPin
            {
                Type = PinType.Place,
                Position = new Position(37.0990243, -7.9982581),
                Label = "Xamarin San Francisco Office",
                Address = "394 Pacific Ave, San Francisco CA",
                Name = "Xamarin",
                Url = "http://xamarin.com/about/",
            };

            customMap.CustomPins = new List<CustomPin> { pin };
            customMap.Pins.Add(pin);
            customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.0990243, -7.9982581),
                 Distance.FromMiles(150)));
dan_udnas
  • 19
  • 7
  • 1
    your VM should not interact directly with your View. That is a core concept of MVVM. Use databinding to link them – Jason Jul 15 '21 at 11:16
  • Can u give me a example on how to bind a custom map pls ? Whitch property to bind ? – dan_udnas Jul 15 '21 at 11:42

1 Answers1

0

I found the Solution:

On Xaml :

<ContentPage.Content>
    <ContentView Content="{Binding customMap}"/>
</ContentPage.Content>

On ViewModel:

public CustomMap customMap { get; private set; }

On ViewModel Constructor:

customMap = new CustomMap();
customMap.MapType = MapType.Street;

           
dan_udnas
  • 19
  • 7
  • this is fine if it works for you, but it is NOT MVVM and is a really bad use of data binding – Jason Jul 15 '21 at 12:29