Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How would you go to let your viewmodel know with which model to work ? #76

Unanswered
stilnat asked this question in Q&A
Discussion options

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 ?

You must be logged in to vote

Replies: 1 comment

Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /