-
-
Notifications
You must be signed in to change notification settings - Fork 30
-
I want to set initial parameter for the viewmodel that is made for a character status.
but it looks like I can't feed my own viewmodel to a CanvasView as it requests the viewmodel when awake.
I'm new to mvvm so teach me how to. thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
You can override the base viewmodel instance creation. Docs.
public class CounterView : CanvasView<CounterViewModel> { // Override the base viewmodel instance creation. // Required in case the viewmodel doesn't have a parameterless constructor. protected override CounterViewModel GetBindingContext() { return new CounterViewModel(69); } }
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks! I saw docs and also checked source code.
I initially thought I can pass some parameters using ctor of CounterViewModel itself as you suggested.
But after digging up VContainer and the MvvmToolkit sample, it looks like I need some kind of Broker or through DI container.
public class CounterView : CanvasView<CounterViewModel>
{
// Override the base viewmodel instance creation.
// Required in case the viewmodel doesn't have a parameterless constructor.
protected override CounterViewModel GetBindingContext()
{
return _scopedContext.Resolve<CounterViewModel>();
//or
return new CounterViewModel(_scopedContext.Resolve<ICounterBroker>().value);
}
}
Beta Was this translation helpful? Give feedback.