Correct Way of Loading Async Data on the ViewModel #18222
-
I'm using Uno with MVVM. What is the correct way to load data after a page is shown? I've tried a few approaches: I tried some things,
This caused several problems, such as data being loaded more than once, leading to bugs with TextBox and ComboBox not being updated correctly. I also tried loading the data in the Page_Loaded event:
But GetRequiredService is returning a new instance of the ViewModel, not the one that was automatically created when the page was opened.
Does Uno support EventToCommandBehavior to call an event in the ViewModel? Can someone please explain the correct approach? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
In |
Beta Was this translation helpful? Give feedback.
-
Indeed, the view model can be accessed using DataContext on the Loaded event. However, this only works on .NET 8-windows10.0.19041; with .NET 8-desktop, the DataContext is null during the Page_Loaded event. On Windows 10, the DataContext is set before the PageLoaded event, while on Desktop, it is set afterwards. Executing LoadData on DataContextChanged is not feasible as it still causes glitches, such as TextBox bugs and missing ComboBox data. |
Beta Was this translation helpful? Give feedback.
-
UnoTextBoxBugDemo.zip Enter a name and then go to second page This will happen with all the TextBoxes. Try running it com Desktop or WebAssembly. If you run it with WinAppSdk, the second screen will not open and will crash with an error. |
Beta Was this translation helpful? Give feedback.
I have created #18228 to track this