Skip to content

Clarify that Page navigation does not guarantee that the DataContext is initialized when the Page is loaded #2629

Open

Description

@ramezgerges

What would you like clarification on

Concern?

  • Usage in industry
  • Clarification of capabilities
  • Getting started with Uno
  • Developing with Uno
  • Contributing to the Uno project
  • Publishing your application
  • Support
  • Other (please specify):

For which Platform

  • iOS
  • macOS (AppKit)
  • Mac Catalyst
  • Android
  • WebAssembly
  • Windows
  • Skia (WPF)
  • Skia (GTK on Linux/macOS/Windows)
  • Skia (Linux Framebuffer)
  • Build tasks

Anything else we need to know?

Activity

  1. added
    kind/consumer-experience
    kind/documentation
    triage/untriaged
    on Nov 28, 2024
  2. ramezgerges commented on Nov 28, 2024

    @ramezgerges
    ContributorAuthor

    cc. Nick

  3. DevTKSS commented on Dec 9, 2024

    @DevTKSS
    Contributor

    would like to add the detail, that for the given case it is not set, we can simply forget to use x:Bind beside Navigation, which needs DI if we want to reach the ViewModel.
    On the main uno repo and discord was told to do:

    Private MainViewModel? ViewModel => (MainViewModel)DataContext;
    

    or:

    Private MainViewModel? ViewModel => DataContext as MainViewModel;
    

    but as soon as I try to bind a task like this:
    image

    no difference if I leave out the Mode, it is throwing a NullReferenceException for the ViewModel! but for things like the simple Title thats not happening at all. Known Issue I heared of would be, twoWay Mode to be complicated, but its the one which is (at least for me) not known to be bugged. So it would be really nice to know how it is working to get the DataContext set in the given case, the navigator fails to do this:
    image

    the page can not take the parameter because the routemaps are not enabling this to be done and the second code on the screenshot from the docs are doing what I did replicate.
    At this point, I would go so far, out of pure desperation, just for getting this ViewModel for the DataContext, to add:

    private MainViewModel? ViewModel => DataContext as MainViewModel ?? this.Navigator()?.Get<MainViewModel>();
    

    Because the x:Bind is applyed at this.InitializeComponent(); if I am correct and seems like the DataContext is not set corrctly as expected. But before you do test it, let me tell you, that is not helping this issue at all, still throwing the same issue at the same line. Hopelessly in my opinion.
    So when it comes to performance and functionallity of binding to this kind of things where it would be nice, its just not usable...
    To solve this, it would be really nice to know what we CAN do, if this happens, if not even the Navigator can pring us this MainViewModel...

  4. DevTKSS commented on Dec 9, 2024

    @DevTKSS
    Contributor

    unoplatform/uno#18710 (comment)
    Similar issue also mentioned there, no responses only "not use it"...

  5. DevTKSS commented on Oct 15, 2025

    @DevTKSS
    Contributor

    @ramezgerges at which time then can we officially expect it to be set correctly in our code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/consumer-experiencekind/documentationtriage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions