Open MartinZikmund opened 4 years ago
After a bit further digging, the async is not the main issue - when we add another page with the same logic and add a button to MainPage
to navigate to it, the second page shows "Should not display" even if OnLaunched
is not async. So it seems for any page that not loaded immediately after start of the application x:Bind behaves "incorrectly"
Added higher priority, as this is important for MvvmCross apps to work seamlessly when Uno support is added.
It seems one of the problems is the ordering of page Loaded and navigation events and overrides, which may be related to https://github.com/unoplatform/uno/pull/2008.
@jeromelaban Oh, I must ping him.
I have also noticed the reason is probably the event ordering - for subsequent navigations, Loaded
event runs before OnNavigatedTo
, which means view model/DataContext is not set yet and hence things like ViewAppearing/ViewAppeared
in MvvmCross stop working.
Leaving here for reference - https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.page.onnavigatedto#remarks - OnNavigatedTo
should always be called before Loaded
This is caused by #2895.
A workaround for this particular issue is to use:
Bindings.Update();
At the end of the OnNavigatedTo method. This will force the bindings to re-evaluate to their newer value (Since OnNavigatedTo is evaluated too late because of https://github.com/unoplatform/uno/issues/2895.
Not sure if related or slightly different, but I'm using the common behavior/load pattern to do async initialization of my ViewModel:
<i:Interaction.Behaviors>
<act:EventTriggerBehavior EventName="Loaded">
<act:InvokeCommandAction Command="{x:Bind ViewModel.InitializeCommand, Mode=OneWay}" />
</act:EventTriggerBehavior>
</i:Interaction.Behaviors>
So while Bindings.Update()
appears to work the first load, this trigger for the Loaded
is being missed on subsequent page navigations in WPF/WASM, where it works fine in UWP.
I may have to see if I can just move this behavior to the OnPageNavigated override instead though, but is another quirk of the loading order being out of order. Will have to test that out later.
Current behavior
When
await
is used before first navigation inApp.xaml.cs
,x:Bind
suddenly behaves "weirdly" - it binds data which are available during page constructor, but does not consider any updates duringOnNavigatedTo
(which is when MVVM frameworks like MvvmCross set the DataContext).Expected behavior
x:Bind
behavior should not change if userawaits
inOnLaunched
.How to reproduce it (as minimally and precisely as possible)
OnLaunched
to be async:OnLaunched
:Workaround
Add the following statement at the end of the
OnNavigatedTo
method:Environment
Nuget Package: latest stable + latest dev
Package Version(s):
Affected platform(s):
Visual Studio:
Relevant plugins:
Anything else we need to know?