C# 棱镜绑定错误的视图模型

C# 棱镜绑定错误的视图模型,c#,mvvm,windows-store-apps,prism,C#,Mvvm,Windows Store Apps,Prism,我刚到公司做了一个项目(在过去的一年里,有将近50个不同的人接触过这个项目…) 我试图修补一些bug,但我遇到了一些问题 我们将prism与MVVM模型一起使用 我得到了一个ContactDetailPage视图,它应该绑定到ContactDetailPageVM,在xaml中有下面一行,我错了吗 prism:ViewModelLocator.AutoWireViewModel="True" 相反,我不知道为什么,它绑定到一个名为ProfilsPagesVM的VM 有人找到了一个在Contac

我刚到公司做了一个项目(在过去的一年里,有将近50个不同的人接触过这个项目…)

我试图修补一些bug,但我遇到了一些问题

我们将prism与MVVM模型一起使用

我得到了一个ContactDetailPage视图,它应该绑定到ContactDetailPageVM,在xaml中有下面一行,我错了吗

prism:ViewModelLocator.AutoWireViewModel="True"
相反,我不知道为什么,它绑定到一个名为ProfilsPagesVM的VM

有人找到了一个在ContactDetailPageVM上强制绑定的解决方案吗

通过网络,没有发现任何与我的问题相关的东西。欢迎任何链接

要帮助您,请参阅ContactDetailPageVM代码:

namespace ViewModels
{
    public class ContactDetailPageVM : BaseViewModel
    {
        private Contact _selectedContact;
        public Contact SelectedContact
        {
            get { return _selectedContact; }
            set { _selectedContact = value;}
        }

        public ContactDetailPageVM()
            : base(_navigationService, _auth, _data)
        {

        }

        public override  void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
            SelectedContact = (Contact)navigationParameter;
        }
    }
}
名称空间视图模型
{
公共类ContactDetailPageVM:BaseViewModel
{
私人联系人_选择联系人;
公共联系人已选择联系人
{
获取{return\u selectedContact;}
设置{u selectedContact=value;}
}
公共ContactDetailPageVM()
:base(\u导航服务、\u身份验证、\u数据)
{
}
public override void OnNavigatedTo(对象navigationParameter、Windows.UI.Xaml.Navigation.NavigationMode NavigationMode、Dictionary viewModelState)
{
base.OnNavigatedTo(navigationParameter、navigationMode、viewModelState);
SelectedContact=(Contact)导航参数;
}
}
}

如果您需要任何其他详细信息,我很乐意与您分享。

AutoWireViewModelChanged逻辑基于命名约定

/// <summary>
/// The ViewModelLocationProvider class locates the view model for the view that has the AutoWireViewModelChanged attached property set to true.
/// The view model will be located and injected into the view's DataContext. To locate the view, two strategies are used: First the ViewModelLocationProvider
/// will look to see if there is a view model factory registered for that view, if not it will try to infer the view model using a convention based approach.
/// This class also provide methods for registering the view model factories,
/// and also to override the default view model factory and the default view type to view model type resolver.
/// </summary>

确保视图模型遵循命名约定,例如ContactDetailPageViewModel

我知道AutoWire的规则,项目中的命名约定是PageNameVM,它适用于所有其他视图,不要认为问题来自名称。它仍然没有解释为什么视图试图绑定ProfilePageVM。编辑:更精确地说,var viewModelTypeName=string.Format(CultureInfo.InvariantCulture,“ViewModels.{0}VM,ViewModels,Culture=neutral”,viewType.Name);这是名称格式的初始化
var viewModelName = String.Format(CultureInfo.InvariantCulture, "{0}ViewModel, {1}", viewName, viewAssemblyName);