-
-
Notifications
You must be signed in to change notification settings - Fork 30
-
Hi, so I'm still wrapping my head around the MVVM pattern, which feels like a right way to do things.
One thing I can't really figure out is how do you get a reference to your model.
From what I've seen, the view create an instance of viewmodel and will set binding on this instance.
You can override the getbinding method to instance a view model using a constructor with parameters, so I figured I should use that to pass it the reference to my model.
But, the view should not know anything about the model so things are getting a bit hairier here. I saw in the samples that usually an appcontext application would handle setting up instances, but I can't do that.
In my case, I need the same class view to be instantiated multiple times for multiple different objects, that can get created or destroyed at runtime. And I want the view to be instantiated whenever the player interact with the object.
I just can't really wrap my head around this.
Here's some code : I have a method that instantiate a prefab containing my view :
private void FilterInteract(InteractionEvent interactionEvent, InteractionReference arg2)
{
Instantiate(FilterViewPrefab);
}
From there, the view instantiate the viewmodel, but then I don't know how to connect my viewmodel to my model. Any clue ?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Hi, In one of my UITK app I use the following code:
var (isCanceled, result) = await _app .ShowDialogAsync<BaseTimerViewModel>(timerViewModel, cancellationToken) .SuppressCancellationThrow();
public async UniTask<DialogResult> ShowDialogAsync<TDialogViewModel>(IBindingContext bindingContext, CancellationToken cancellationToken = default) where TDialogViewModel : class, IBindingContext { var dialogView = _dialogsService.GetDialogView<TDialogViewModel>(); var dialogRoot = ((VisualElement) dialogView).parent; try { dialogRoot.SetChildsBindingContext(bindingContext, _objectProvider); await _dialogContainer.ShowAsync(dialogView, dialogRoot, cancellationToken); return dialogView.Result; } finally { dialogRoot.ResetChildsBindingContext(_objectProvider); } }
If you use UGUI, you will need to implement the SetChildsBindingContext
and ResetChildsBindingContext
methods yourself. See VisualElementExtensions.cs for an example.
Beta Was this translation helpful? Give feedback.