xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] Can't change BindingContext of a MenuItem Page #13498

Open GeorgeVelikov opened 3 years ago

GeorgeVelikov commented 3 years ago

Description

Setting the BindingContext of a page with some null BindingContext and then pushing it seems to turn back to the "inherited" BindingContext

Steps to Reproduce

  1. Create Screen
  2. Register Screen with some route i.e. Routing.RegisterRoute("About", typeof(AboutScreen))
  3. Add MenuItem to shell which has the Routing.Route to "About", BindingContext is of some AboutScreenViewModel
  4. On MenuItem_Clicked, grab the page and assign the page BindingContext to the MenuItem BindingContext
    • We can do this with Routing.GetOrCreateContent("About")
    • Calling MenuItem.BindingContext does return the correct BindingContext (AboutScreenViewModel)
  5. Push Page (this is where the issue occurs)
    • This is done with the Shell's INavigation object, i.e. shellNavigiationStack.PushAsync(aboutPage, isAnimated)
  6. Error as the BindingContext of the AboutScreen is not AboutScreenViewModel but is the BindingContext of the Shell.

If you're using my repro

  1. Start app
  2. Open flyout
  3. Press "Broken Page" menu item
  4. 🧨 boom 🔥, BindingContext is not changed although I have explicitly changed it.

Expected Behavior

Pushed Page to have the manually set BindingContext

Actual Behavior

Pushed Page has the inherited BindingContext

Basic Information

Workaround

N/A, couldn't figure a workaround for this, keeping my app at 4.8 SR 6 for now

Reproduction

The page in question is "BrokenPage"

XF5ShellIssues.zip

PureWeen commented 3 years ago

@GeorgeVelikov can you attach a repro please?

GeorgeVelikov commented 3 years ago

@PureWeen Yeah, I'll create one some time tomorrow GMT

GeorgeVelikov commented 3 years ago

@PureWeen I have added a reproduction for this issue.

The page in question is "BrokenPage"

XF5ShellIssues.zip

GeorgeVelikov commented 3 years ago

@PureWeen the issue is coming from the TemplateBinding in the ContentPageTemplate in App.xaml

If I remove the template binding, then everything works as expected.

Edit: However it breaks some functionality I rely on within the control template (so it doesn't actually work as expected)

This seems to be related to https://github.com/xamarin/Xamarin.Forms/issues/13497

I am using a template binding in a ControlTemplate in this case, and wouldn't have expected an issue

GeorgeVelikov commented 3 years ago

Update:

So it looks like this is an issue with the binding context being set on the content page once it's added in the visual tree. The binding context is overriden by the inherited binding context.

  1. Create a content page
  2. Set binding context to content page
  3. Push content page
  4. Xamarin.Forms overrides the content page binding context with the inherited one