[Enhancement] ViewModel add OnApearing like methods for async/await invoke #120
Replies: 13 comments 7 replies
-
Prism already has this with Essentially, if Maui were to implement something similar, they could just extend the if(element.BindingContext is IPageLifecycleAware lifecycleAware)
{
lifecycleAware.OnAppearing();
} Then your view model would just implement the interface public class MyFunViewModel : IPageLifecycleAware
{
public void OnAppearing() { }
public void OnDisappearing() { }
} note: interface name notwithstanding |
Beta Was this translation helpful? Give feedback.
-
FWIW, I wouldn't complain about not having to support that in Prism for MAUI |
Beta Was this translation helpful? Give feedback.
-
I don't know if this belogns here but, it would be nice too if passing parameters direct to ViewModel, when navigating between pages, was implemented from the begining. Like queryProperties that shell(link here) already has in xamarin. |
Beta Was this translation helpful? Give feedback.
-
You can write small navigation service:
|
Beta Was this translation helpful? Give feedback.
-
@VladislavAntonyuk that it's what I actually do. |
Beta Was this translation helpful? Give feedback.
-
I totally disagree with the idea of adding page methods to view model. We have code behind for that. VM shouldn’t know anything about the page and can be applied to any page |
Beta Was this translation helpful? Give feedback.
-
I agree with @VladislavAntonyuk Nothing stops me from using the very same ViewModel instance in two different views at the same time. How would the proposed lifecycle interface help in this case? |
Beta Was this translation helpful? Give feedback.
-
Just reviving this old thread in case anyone is watching... I think .NET MAUI should have an opinionated way of asynchronously initialising events for a page. We have some lifecycle methods in the
Other UI frameworks provide methods for handling this. For example, if you look at Angular, you can subscribe to something in your In short, I want the How difficult would it be to implement something like this in .NET MAUI? cc: @jfversluis |
Beta Was this translation helpful? Give feedback.
-
I am NOT sure if this is the correct way to handle it, but I think it works OK. Using CommunityToolkit.MVVM v8.
|
Beta Was this translation helpful? Give feedback.
-
I'm surprised and happy that this idea will be shown to me again at that moment. At last, if you go to look at others ideas I raised, like this idea, I just suggest that it is a virtual method, that's mean: hey the framework is good that provide enough features to us to do what I want to do. But that's will not bother me because it is virtual, it is just a options for me like prismframework do. |
Beta Was this translation helpful? Give feedback.
-
I've ran into a similar problem where I need to load data asynchronously after a view model is created. On other posts, some suggest creating the task to load the data in the view model's constructor. I do not like that option because getting the final load onto the main thread is easy to mess up. I agree with the discussions in this post to call the init code in the view's OnAppearing method. I chose to have the view model implement an interface so that it could still inherit from a class if needed
Cons:
Pros:
Notes: |
Beta Was this translation helpful? Give feedback.
-
I'm starting to think this might be a documentation issue. In many discussions the .NET MAUI team are encouraging people to perform page initialisation tasks in (or triggered from) the I would still like the framework to have an opinionated way of doing this, because it's such a ubiquitous requirement. But in the absence of that, if |
Beta Was this translation helpful? Give feedback.
-
I'm quite interested in this as well and stumbled on this repo https://github.com/Keflon/Maui.MvvmZero and are now using it as a Prism replacement for MVVM navigation and taking advantage of page lifecycles in our view model pages. Without going into all the details it has available MVVM navigation is supported public async Task<TViewModel> PushVmAsync<TViewModel>(Action<TViewModel> initViewModelAction, Which seems to be a similar to how Popups pushes the popup view model this.popupService.ShowPopup<UpdatingPopupViewModel>(onPresenting: viewModel => viewModel.PerformUpdates(10)); To more easily support swapping out 3rd party plugins we created a public class NavigationService(IPageServiceZero pageServiceZero) : INavigationService
{
TViewModel INavigationService.FindAncestorPageVm<TViewModel>() => pageServiceZero.FindAncestorPageVm<TViewModel>();
Task<TViewModel> INavigationService.NavigateAsync<TViewModel>(Action<TViewModel>? initViewModelAction, bool isModal)
{
initViewModelAction ??= static vm => vm.Initialize(null);
return pageServiceZero.PushVmAsync(initViewModelAction, isModal: isModal);
}
Task INavigationService.PopAsync(bool isModal, bool animated) => pageServiceZero.PopAsync(isModal, animated);
Task INavigationService.PopToRootAsync() => pageServiceZero.PopToRootAsync();
public void RemovePageBelowTop() => pageServiceZero.RemovePageBelowTop();
}
Usage is as follows with our own custom await navigationService.NavigateAsync<MyPageViewModel>(vm => vm.Initialize(new NavigationParameters
{
{ NavigationParameterKeys.RandomParamKey, "The Value" }
})).ConfigureAwait(false); |
Beta Was this translation helpful? Give feedback.
-
Summary
In traditional way , something async operations can be invoke at OnApearing method in Page.cs , but
something async operations I want use on viewmodel after the constructor method. Maybe refer the PrismFramework but maybe MAUI can do this without Inherited some class from App.cs
API Changes
Provide some interface method to implement or abstract method to override in viewmodel.
Intended Use Case
Viewmodel can do something async/await operations after its constructor finished.
Beta Was this translation helpful? Give feedback.
All reactions