We have a Xamarin.Forms app with FreshMvvm. Now, as Xamarin.Forms will not get support beginning next year, I am re-writing the app with .Net Maui. For MVVM pattern, I am trying to use CommunityToolkit.Mvvm. But I wonder how I can initialize the viewmodel now. With FreshMvvm I could override Init(), but CommunityToolkit.Mvvm does not seem to have anything like this. What is the right way to initialize the viewmodel asynchronously, as there is no async constructor?
How to initialize ViewModel with CommunityToolkit.Mvvm
2.9k views Asked by David Shochet At
2
There are 2 answers
5
Alexandar May - MSFT
On
In FreshMVVM, since the model has impelmented the FreshBasePageModel, you can override the Init() method and initialize data like the pseudo code below:
public override void Init (object initData)
{
//initialize data
}
However, in CommunityToolkit.Mvvm, you can set the data whatever you want in the default constructor like below:
public partial class MainPage : ContentPage
{
public MainPage(MainPageViewModel vm)
{
InitializeComponent();
BindingContext = vm;
Initialize();
}
public async void Initialize()
{
//await operation
}
}
Official reference link:https://learn.microsoft.com/en-us/windows/communitytoolkit/mvvm/introduction
Related Questions in MAUI
- Incorrect display of LinearGradientBrush in IOS
- how to migrate native android studio project to MAUI?
- In MAUI how can I Show different appshell tabbars depending on the user role?
- Visual Studio throwing DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION when I try to add a log
- Deeplinking in .net Maui "Window was allready created"
- MauiApp crashes only in release mode at sideswitch android.os.DeadObjectException
- How to display google maps in dark mode in a .NET MAUI (Android) app
- Move .net maui contents below the drawer when it's opened
- App is crashing and getting Java.Lang.IllegalArgumentException: Cannot setMaxLifecycle for Fragment not attached to FragmentManager
- NavbarAnimation.Maui With TabbedPage
- Maui custom iOS picker renderer conversion
- MAUI slider with a tooltip implementation
- Both `TouchBehavior.CommandParameter` and `TouchBehavior.LongPressCommandParameter` are always passed as `null` to `Command` and `LongPressCommand`
- How do I change the size of a window?
- How to pass the registered data from Signup Page to Setting Page, .NET MAUI
Related Questions in FRESHMVVM
- Switch from Xamarin Forms FreshMVVM to MAUI CommunityToolkit.MAUI
- How to bind SwipeItem in Xamarin using FreshMvvm
- Opening a Page in FreshMVVM from a Push Notification and onward Navigation
- How to initialize ViewModel with CommunityToolkit.Mvvm
- Xamarin.Forms/FreshMVVM - Yes/no style popup
- Xamarin: Bind picker selected item with FreshMvvM ViewModel
- In Xamarin Forms application in MVVM, i use FreshMvvm framework and want to navigate to targetPageModel and pass data from commandParameter
- Dynamic CollectionView with FreshMvvm Xamarin.Forms
- Xamarin.Forms with FreshMVVM. Adding new property to FreshBasePageModel class
- Xamarin trigger on button not firing in a FreshMvvm / SwipeView context
- Xamarin forms page not loaded properly for PopPageModel using navigation stack in FreshMVVM
- PushPageModel not working on simple button in Xamarin
- How to Add a Floating Action Button to Bottom Navigation Bar in IOS (Xamarin)?
- Xamarin FreshMVVM PublicKeyToken=null not found'
- FreshMvvm lifecycle issue
Related Questions in MAUI-COMMUNITY-TOOLKIT
- Both `TouchBehavior.CommandParameter` and `TouchBehavior.LongPressCommandParameter` are always passed as `null` to `Command` and `LongPressCommand`
- Not showing Created folder in My App directory MAUI
- .net Maui: How to bind SelectedItem in a CollectionView to a command / command parameter in view model in code, not xaml
- Hide keyboard after Focus event
- Does the [NotifyCanExecuteChangedFor] Annotation Not Work in Combination with ObservableCollection?
- Can't play H264 video created with ffmpeg libx264 library with Maui Community MediaElement
- how to develop Super Apps using MAUI
- CommunityToolkit.Maui Popup fullscreen throws error
- Using EventToCommandBehavior with a gesture recognizer
- How to add title on top of shell and how change the TabBar size on maui shell?
- Binding properties on Picker with .NET MAUI MVVM
- Can a WeakReferenceMessenger message be subscribed to in a ViewModel's constructor before being navigated to?
- Cancel Image.Source replacement if the condition in the Drop command is not matched
- Entry completed event not firing on Android with carriage return from barcode scan; works on Windows
- How to update Progress from state of ObservableCollection<T>?
Related Questions in COMMUNITY-TOOLKIT-MVVM
- Error XFC0045 - How to bind a CarouselView to ObservableCollection in Maui?
- Maui MVVM - NotifyPropertyChangedFor not working with complex object and its child property
- MAUI MVVM - How to populate with data returned from NOSQL into CollectionView
- WPF Create a reuseable UserControl that binds to ObservableCollection<float>
- How do I display an image in <Image> in WinUI 3?
- Maui - How to attach the pincode to a view model using MVVM
- How do I handle a view that loads for a very long time?
- In .Net Maui using MVVM - How to enable/disable a button based on value in entry field?
- Using the community toolkit with xamarin iOS and Xamarin Android
- Share record to community based on condition
- Fire RelayCommand from DataGrid with AvaloniaUI and Community Toolkit MVVM
- WPF MVVM: ObservableValidator with Model in backing field
- NotifyCanExecuteChangedFor not working when added to an object in MAUI 8.0
- How to Call Methods in the Setter Property of [ObservableProperty] in CommunityToolkit.MVVM?
- Visualize UI Validation Errors on Controls using MVVM CommunityToolkit
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
I started with .net Maui a weeks ago and also needed the possibility to run async code in the constructor of the view model. I found the method
SafeFireAndForgetfromAsyncAwaitBestPractices(nuget package). The async code will run in a different thread and the constructor may will complete before the async method.