0
votes

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 .

1
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
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
@Jason that the code uses PRISIM, where do you usually look for the binding when PRISM is used?Tlink
@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
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

1 Answers

1
votes

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.