how to create viewmodel with param
See original GitHub issueI use a viewmodel like
public class RootViewModel(Func<NextViewModel> func, IWindowManager wndManager>
{
public void ShowNext()
{
wndManager.ShowWindow(func());
}
}
It works well. But if I want to pass a param to the NextViewModel
, It cannot work.
public class RootViewModel(Func<int, NextViewModel> func, IWindowManager wndManager>
{
public void ShowNext(int i)
{
wndManager.ShowWindow(func(i));
}
}
It give me a Unable to find a constructor for type RootViewModel which we can call:
error.
How can I pass the param to the viewmodel?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Android ViewModel additional arguments - mvvm
initialize(param) in onCreate of the activity, for example, it can be called multiple times on the same MyViewModel instance as the user rotates ......
Read more >Create ViewModels with dependencies
Create ViewModels with dependencies Part of Android Jetpack. ... ViewModels can take dependencies as parameters in their constructor.
Read more >Advanced ViewModels (part I): Dependencies and Passing ...
ViewModel as the bridge between the View and the Model. TL;DR: We can pass parameters to our ViewModel, use it as a data...
Read more >How To Use ViewModels Delegate With Constructor ...
But first, let us take a look at the recommended approach for creating a ViewModel with a ViewModelProvider.Factory to be able to use...
Read more >Parameter Injection for Android ViewModels | by Alex Frank
We install the module in ViewModelComponent , making the parameter available for the lifetime of the ViewModel it is injected in. The actual...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The ioc container provided with Stylet isn’t powerful enough to create factories which take arbitrary parameters.
One option is to switch to another ioc container – there’s info on this in the docs.
Another option is to write your own factory:
Another option is to just instantiate the ViewModel yourself:
This isn’t really an issue with Stylet - it’s a question about dependency injection and ioc containers. You don’t have to use dependency injection, and you don’t have to use Stylet’s (simple) ioc container.
Closing as no response