0

Further to This Question regarding BindingContext

I'm looking at a Xamarin.Forms project that implements MVVM , when I look into a XAML page, for example SomePage.xaml and I want to find the ViewModel that has the logic for that page, it's usually a hit and a miss.

In simpler Xamarin.Forms applications I would just look into SomePage.xaml.cs to see the bindingContext.

But it seems in a bigger app this is "Abstracted Somewhere"

For example in SomePage.xaml I see :

x:Class="MyApp.SomePage"

But this is usually not the class containing the code, it seems to be the code representation of the XAML file

I had more luck when I search for SomePageViewModel.cs, but not all the time.

Searching for the term bindingContext in the code yielded no results.

My question is where else can I look for bindingContext , to determine what classes contains the code controlling the XAML pages.

It seems that this is abstracted in some way because I see in the code BindableBase .

Tlink
  • 875
  • 2
  • 14
  • 30
  • If there's no `BindingContext` set in the .cs or in the .xaml I would search for references of the page because they might be creating the page and setting the context there before pushing the page. – Nick Peppers May 23 '19 at 16:56
  • it could be set in the XAML, or they could be using a library like PRISM that handles it. WIthout knowing the specific example you're looking at it's impossible to say. – Jason May 23 '19 at 17:01
  • @Jason that the code uses PRISIM, where do you usually look for the binding when PRISM is used? – Tlink May 23 '19 at 17:13
  • @Nick so I take it that there could be one class that declares the pages and their bindings, but then I should be able to find that file when i search by page name, which is not the case. – Tlink May 23 '19 at 17:17
  • If the View has a parent class it might be being set there. Check how Views are instantiated in the application. There's abstractions like dependency injection that might make it harder to find dependencies. Also there's a pattern called ViewModel first, where the View is created when an Instance of the ViewModel is created... – Esteban Verbel May 23 '19 at 17:20
  • https://prismlibrary.github.io/docs/xamarin-forms/Getting-Started.html#view-models – Jason May 23 '19 at 17:20
  • Oh also... there's an Override method on ContentPages called OnBindingContextChanged. If you added to your SomethingPage and set a break point there you will be able to see on the CallStack where it's happening – Esteban Verbel May 23 '19 at 17:21
  • Great thanks @Jason @EstebanVerbel @Nick , reading through PRISIM samples it looks very similar to what I see in the app. and I can also see the `OnBindingContextChanged` – Tlink May 23 '19 at 17:33

1 Answers1

1

from the PRISM docs

prism:ViewModelLocator.AutowireViewModel="True"

This view (MainPage.xaml) is wired to the view model (MainPageViewModel.cs) automatically via naming conventions allowing for databinding to the view model. See ViewModelLocator documentation for more information.

and

Within the Portable project there is a ViewModels folder. This folder will contain all of your view model related code. The template created a view model for the MainPage called MainPageViewModel.cs in this folder. Lets take a look at this class and break down what is going on here.

Jason
  • 86,222
  • 15
  • 131
  • 146