✅ Need help understanding parent and child views
So basically, spent the past four hours learning thoroughly about the MVVM design pattern because there was a problem with my application. Although i did get some insight into it, i was not able to overcome the hurdle i have. Hence i'd like someone to help me through my situation. I saw a lot scenarios gluing views and models using view models. But did not particulary come across the problem i had. So basically, i have a main view and a child view. The child view does not inherit from the main view nor does it get any data from the main view model. The parameters in both views although are triggered by a button on the main view. I was able to contain the variables that each view needed to their respective viewmodels. I have a problem with linking the both dataupdate method calls to the button click event. But before that problem, I have problems properly displaying the data made in the child view model on the application. I'm sure it has to do with the way i'm binding the view to the main view but i am not experienced enough to determine the fault in that. Please i require some assistance :pepecry:
34 Replies
BlazeBin - bxhhutrtlepf
A tool for sharing your source code with the world!
It has both the mainviewmodel and the homeview model
the home view model is the nested view
Just to add, the home view is bound to a content control
by binding the content to currentview as shown here
AsyncRelayCommand - Community Toolkits for .NET
An asynchronous command whose sole purpose is to relay its functionality to other objects by invoking delegates
Whaat task are you refering to
I'm pretty sure all the variables for the binding are being populated at runtime and after the button push event
Both
You just assign the async task to a discarded task variable, so basically your command does nothing
you mean this?
i get errors doing this. I was just trying stuff out
Yes
but the commented line actually displays the contents to my application
Like this, the info on the right side shows up
I dont know how to trigger the other method from the home view model
Just use the command type I shared above
Is it a synchronous task?
Yah
Wait, can i bind more than one command to the button?
Guess that's why it works, but then it's no different than a regular void-returning method
Wait non, its async
No, I don't think so
Hmm, then I probably don't know enough about how tasks work
If i could bind more than one command to a button it might be a bit easir to solve this problem
Either way, if you need async commands, use the appropriate type (AsyncRelayCommand)
The command itself is not async. It just calls an async method
But if you have any idea on how to call the second method from the homeview model, i'd like to hear it
I mean if you need a command to run async code, use
AsyncRelayCommand
I guess if that's the properway i'll change mine
You mean the
UpdateWeeklyData
one?Yah
What errors are you having with it?
it was a null reference exception
The homeVM reference passed in was empty
here
did i have to make a new instance of homeVM in the HomeViewModel before passing it as a reference here
It's supposed to be a parameter you assign in xaml. The property is
CommandParameter
First I've heard of, can you elaborate
Buttons have a
Command
property that you already seem to be using, and a CommandParameter
that you can use to pass some data every time the bound command is executed
So because you didn't set CommandParameter
in xaml, you get a NullReferenceException
Currently looking into it
@nohopestage IT ACTUALLY WORKED
LETS FUCKING GOOOO"
Glad to hear! What did you do to fix it?
basically made new async relay command and passed into it a method that contained my dataupdate methods
That's all i needed to do
basically this
You can also remove the attribute now
Oh ya, lol
Thanks man
Been having this problem for a while now and because of you i have a better understanding of mvvm
Glad I could help!