简体   繁体   中英

When using Prism on Xamarin, how can I warn if the names on the View, and ViewModel don't match?

Prism uses MVVM style convention naming to bind a View to a ViewModel on an Xamarin application.

I recently altered some of the demo code and realized, after spending way too much time debugging, that the pagename didn't match the view.

How can I enable logging, or pre compiler warnings that such a discrepancy exists?

Locating the ViewModel for the View is handled in the Prism Core by the ViewModelLocationProvider. You can see here how it is implemented. If you which to override the default behavior and inject your own logging you can use that as a template and call ViewModelLocationProvider. SetDefaultViewTypeToViewModelTypeResolver(YourCustomerLocatorFunctionHere) ViewModelLocationProvider. SetDefaultViewTypeToViewModelTypeResolver(YourCustomerLocatorFunctionHere) in your App.OnInitialized .

As for any any precompile warning you'd be on your own there to write a tool.

To add to Dan's response, you can bypass the naming convention and register the ViewModel directly with the View by using the Container.RegisterTypeForNavigation<View, ViewModel>() method. This is also faster as reflection will not be used to locate the VM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM