6

Below is the definition of my page in Xamarin Forms with Prism MVVM framework:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" prism:ViewModelLocator.AutowireViewModel="True" xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps" x:Class="MyProject.UI.Modules.Views.MapPage"> ..... </ContentPage> 

Unfortunately Xamarin maps are not MVVM-ready when it comes to binding pins, etc. Therefore, I need to make some code changes in the C# code behind of the page. How can I access the instance of the ViewModel interacting with this page in the code behind?

1 Answer 1

9

You could always monitor for property changes on the ViewModel and use the values to update the map. To get a hold of the ViewModel, just cast your BindingContext

Example : ((MainPageViewModel)this.BindingContext)

Example link : https://github.com/PrismLibrary/Prism-Samples-Forms/blob/7b0ce9ca31f07dea5020dbd5875d16f18bcdf09a/ContosoCookbook/ContosoCookbook/ContosoCookbook/Views/MainPage.xaml.cs#L16

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.