C
C#11mo ago
Kingdawanage

✅ 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
Kingdawanage
KingdawanageOP11mo ago
BlazeBin - bxhhutrtlepf
A tool for sharing your source code with the world!
Kingdawanage
KingdawanageOP11mo ago
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
public MainViewModel()
{
HomeVM = new HomeViewModel();
CurrentView = HomeVM;
}
public MainViewModel()
{
HomeVM = new HomeViewModel();
CurrentView = HomeVM;
}
nohopestage
nohopestage11mo ago
The task doesn't run because it's not awaited. Use AsyncRelayCommand instead
AsyncRelayCommand - Community Toolkits for .NET
An asynchronous command whose sole purpose is to relay its functionality to other objects by invoking delegates
Kingdawanage
KingdawanageOP11mo ago
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
nohopestage
nohopestage11mo ago
Both You just assign the async task to a discarded task variable, so basically your command does nothing
Kingdawanage
KingdawanageOP11mo ago
public void SearchButtonPressed(HomeViewModel homeVM)
{
//Task _ = UpdateCurrentData();
Task _ = homeVM.UpdateWeeklyData(this);
}
public void SearchButtonPressed(HomeViewModel homeVM)
{
//Task _ = UpdateCurrentData();
Task _ = homeVM.UpdateWeeklyData(this);
}
you mean this? i get errors doing this. I was just trying stuff out
nohopestage
nohopestage11mo ago
Yes
Kingdawanage
KingdawanageOP11mo ago
but the commented line actually displays the contents to my application
Kingdawanage
KingdawanageOP11mo ago
Like this, the info on the right side shows up
No description
Kingdawanage
KingdawanageOP11mo ago
I dont know how to trigger the other method from the home view model
nohopestage
nohopestage11mo ago
Just use the command type I shared above Is it a synchronous task?
Kingdawanage
KingdawanageOP11mo ago
Yah Wait, can i bind more than one command to the button?
nohopestage
nohopestage11mo ago
Guess that's why it works, but then it's no different than a regular void-returning method
Kingdawanage
KingdawanageOP11mo ago
Wait non, its async
nohopestage
nohopestage11mo ago
No, I don't think so Hmm, then I probably don't know enough about how tasks work
Kingdawanage
KingdawanageOP11mo ago
If i could bind more than one command to a button it might be a bit easir to solve this problem
nohopestage
nohopestage11mo ago
Either way, if you need async commands, use the appropriate type (AsyncRelayCommand)
Kingdawanage
KingdawanageOP11mo ago
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
nohopestage
nohopestage11mo ago
I mean if you need a command to run async code, use AsyncRelayCommand
Kingdawanage
KingdawanageOP11mo ago
I guess if that's the properway i'll change mine
nohopestage
nohopestage11mo ago
You mean the UpdateWeeklyData one?
Kingdawanage
KingdawanageOP11mo ago
Yah
nohopestage
nohopestage11mo ago
What errors are you having with it?
Kingdawanage
KingdawanageOP11mo ago
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
nohopestage
nohopestage11mo ago
It's supposed to be a parameter you assign in xaml. The property is CommandParameter
Kingdawanage
KingdawanageOP11mo ago
First I've heard of, can you elaborate
nohopestage
nohopestage11mo ago
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
Kingdawanage
KingdawanageOP11mo ago
Currently looking into it @nohopestage IT ACTUALLY WORKED LETS FUCKING GOOOO"
Kingdawanage
KingdawanageOP11mo ago
No description
nohopestage
nohopestage11mo ago
Glad to hear! What did you do to fix it?
Kingdawanage
KingdawanageOP11mo ago
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
public MainViewModel()
{
HomeVM = new HomeViewModel();
CurrentView = HomeVM;
UpdateCommand = new AsyncRelayCommand(SearchButtonPressed);
}
public MainViewModel()
{
HomeVM = new HomeViewModel();
CurrentView = HomeVM;
UpdateCommand = new AsyncRelayCommand(SearchButtonPressed);
}
[RelayCommand]
public async Task SearchButtonPressed()
{

await UpdateCurrentData();
await HomeVM.UpdateWeeklyData(SearchBoxText);
}
[RelayCommand]
public async Task SearchButtonPressed()
{

await UpdateCurrentData();
await HomeVM.UpdateWeeklyData(SearchBoxText);
}
nohopestage
nohopestage11mo ago
You can also remove the attribute now
Kingdawanage
KingdawanageOP11mo ago
Oh ya, lol Thanks man Been having this problem for a while now and because of you i have a better understanding of mvvm
nohopestage
nohopestage11mo ago
Glad I could help!
Want results from more Discord servers?
Add your server