我有一个使用服务定位器模式的MvvmLight的Xamarin Forms应用程序。 ViewModelServiceLocator本身是一个静态类,可以访问已注册所有依赖项的Autofac容器。每个虚拟机都有一个静态属性:
public static AboutViewModel AboutVm => ServiceLocator.Current.GetInstance<AboutViewModel>();
我的应用程序中有定位器设置,如下所示:
<presentation:ViewModelLocator x:Key="Locator" />
在页面上的xaml中:
BindingContext="{Binding AboutVm, Source={StaticResource Locator}}"
但是从未调用过定位器。 但是,当我通过
在后面的代码中进行设置时,它确实可以工作BindingContext = ViewModelLocator.AccountListVm;
我错过资源定义或其他内容吗?