以下是使用Prism MVVM框架的Xamarin Forms中我的页面的定义:
<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>
不幸的是,当绑定引脚等时,Xamarin映射不支持MVVM . 因此,我需要在页面后面的C#代码中进行一些代码更改 . 如何在后面的代码中访问与此页面交互的ViewModel实例?
1 回答
您始终可以监视ViewModel上的属性更改,并使用这些值更新映射 . 要获得ViewModel,只需转换BindingContext
示例:
((MainPageViewModel)this.BindingContext)
示例链接:https://github.com/PrismLibrary/Prism-Samples-Forms/blob/master/ContosoCookbook/ContosoCookbook/ContosoCookbook/Views/MainPage.xaml.cs#L16