uno.extensions
uno.extensions copied to clipboard
Clarify that Page navigation does not guarantee that the DataContext is initialized when the Page is loaded
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?
cc. Nick
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:
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:
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...
https://github.com/unoplatform/uno/issues/18710#issue-2636783971 Similar issue also mentioned there, no responses only "not use it"...
@ramezgerges at which time then can we officially expect it to be set correctly in our code?