Search code examples
c#xamarinmvvmcustom-renderer

Acces x:Name from CustomMap on ViewModel MVVM Xamarin


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

Solution

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