Replies: 9 comments 7 replies
-
I'm facing the exact same problem. Did you find a solution since? |
Beta Was this translation helpful? Give feedback.
-
Can you run LoadAsync in Task.Run() and not await, just make sure you do the updates on the main UI thread. Just a word of caution, using Shell.Current.GoToAsync is super awkward with string routes for passing data. From time to time you will want to pass in objects which might solve this issue in this case. I find pushing the page works better personally. |
Beta Was this translation helpful? Give feedback.
-
Hi there! |
Beta Was this translation helpful? Give feedback.
-
Did anyone find a good solution for this that isn't Task.Delay that makes the navigation not stutter? I tried all combinations of Task.Run, Dispatcher.DispatchAsync etc. and the only thing that reliably works is Task.Delay, which kinda sucks because I'd like to start loading the data from the viewmodel immediately when it is instantiated/appearing, instead of waiting for some arbitrary delay. |
Beta Was this translation helpful? Give feedback.
-
Load the data whenever you want but asynchronously, once you have the data trigger re-rendering. Usually re-render happens automatically via data binding but you can trigger it manually. So for example you should display a loading page or simply empty grid initially but after loading data you get stuffs displayed Executing the loading asynchronously afaik shouldn't cause UI lag, and if you are triggering re-rendering after finished data loading, use marshaling (as required by .NET products mostly) to update the UI by calling a special function in the control but I forgot which |
Beta Was this translation helpful? Give feedback.
-
I tend to use either the Reference Article. Understanding .NET MAUI Page and Navigation Lifecycle Event Order |
Beta Was this translation helpful? Give feedback.
-
I think everyone commenting on this or those unsure what/when/how to do this should take a look at this talk. |
Beta Was this translation helpful? Give feedback.
-
Try |
Beta Was this translation helpful? Give feedback.
-
It's Task.Run that's problematic If Task.API is single threaded asynchronous api it wouldn't be so problematic, people could've run all tasks in threads if they wanted, but the library is designed badly to be used in C# with async await keywords so parallelism is mixed in the use scenarios and is not handled elegantly I'm saying something wrong so |
Beta Was this translation helpful? Give feedback.
-
Hey there.
Can someone explain to me when to load data in a viewmodel to avoid a stuttering navigation animation?
I navigate to a page using:
await Shell.Current.GoToAsync($"//{nameof(xxxxx)}", true);
In my page I override the "OnAppearing" event.
protected override async void OnAppearing() { base.OnAppearing(); await ViewModel.InitializeAsync(this); }
The InitializeAsync method in my viewModel looks like this...
public override async Task InitializeAsync(object sender) { IsBusy = false; HasError = false; ErrorMessage = string.Empty; IsBusy = true; await LoadAsync(); IsBusy = false; }
The "LoadAsync" method loads data from a rest service and binds the result to i.e. an obserableCollection.
Using this code, there is some sort of "stuttering" when navigating to the page.
When I add
await Task.Delay(500);
before calling "LoadAsync" the stuttering goes away. The delay depends on the number of data being loaded. Can be 500, 250 or something else.Is there a better way without using Task.Delay?
Thanks a lot for your help!
Beta Was this translation helpful? Give feedback.
All reactions