首页 > 解决方案 > 在 XAML 中绑定到绑定定位器不起作用

问题描述

我有一个使用服务定位器模式的带有 MvvmLight 的 Xamarin Forms 应用程序。ViewModelServiceLocator 本身是一个静态类,可以访问注册了所有依赖项的 Autofac 容器。对于每个 VM,都有静态属性:

    public static AboutViewModel AboutVm => ServiceLocator.Current.GetInstance<AboutViewModel>();

我在我的应用程序中设置了定位器,资源如下:

<presentation:ViewModelLocator x:Key="Locator" />

在我页面上的 xaml 中:

BindingContext="{Binding AboutVm, Source={StaticResource Locator}}"

但是永远不会调用定位器。但它确实有效,当我在后面的代码中设置它时

BindingContext = ViewModelLocator.AccountListVm;

我错过了资源定义还是什么?

标签: xamarin.formsservice-locatorcommon-service-locator

解决方案


推荐阅读