How to access an instance of a view model in the XAML page code behind (Xamarin Forms) in Prism

The following is the definition of my page in Xamarin formats with a 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 cards are not MVVM-ready when it comes to pinning contacts, etc. So I need to make some code changes to the C # code behind the page. How can I access the instance of ViewModel interacting with this page in code?

+4
source share
1 answer

You can always track property changes in the ViewModel and use the values ​​to update the map. To get hold of the ViewModel, just cast your BindingContext

Example: ((MainPageViewModel)this.BindingContext)

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

+8

Source: https://habr.com/ru/post/1668342/


All Articles