question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to create viewmodel with param

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
canton7commented, Sep 3, 2020

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:

public class NextViewModelFactory
{
    public QueryViewModel CreateQueryViewModel(JiraQuery query)
    {
        return new QueryViewModel(....);
    }
}

public class RootViewModel(NextViewModelFactory factory, IWindowManager wndManager>
{
   public void ShowNext()
  {
    wndManager.ShowWindow(factory.CreateQueryViewModel(...));
  }
}

Another option is to just instantiate the ViewModel yourself:

public class RootViewModel(NextViewModelFactory factory, IWindowManager wndManager>
{
   public void ShowNext()
  {
    wndManager.ShowWindow(new QueryViewModel(...));
  }
}

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.

0reactions
canton7commented, Nov 28, 2020

Closing as no response

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found