棱镜绑定错误的viewmodel

时间:2014-10-13 11:36:57

标签: c# mvvm windows-store-apps prism

我刚刚参加了我公司的一个项目(这个项目在过去的一年中有近50个不同的人接触过这个代码......)

我正在尝试修补一些错误,但我遇到了一些问题。

我们正在使用带有MVVM模型的棱镜。

我有一个ContactDetailPage视图应该与xaml中的以下行绑定到ContactDetailPageVM,我错了吗?

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;
        }
    }
}

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

1 个答案:

答案 0 :(得分:1)

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>

ViewModelLocationProvider.cs

var viewModelName = String.Format(CultureInfo.InvariantCulture, "{0}ViewModel, {1}", viewName, viewAssemblyName); 

确保您的视图模型遵循命名约定,例如ContactDetailPage的视图模型