❔ IEnumerable to ObservableCollection

I am not sure if im doing this correctly but I am trying to create a sqlite table with some data in it and then load the data from the table. I havent tried to run the code yet because when I follow along with the tutorial the code he writes pulls the list as a IEnumerable but I have the list as an observable collection so that the UI will change when the list is updated. Is there a way to change IEnumerables to ObservableCollections? And is that even what I should do here? Thanks https://paste.mod.gg/cpttdpqbguai/0
BlazeBin - cpttdpqbguai
A tool for sharing your source code with the world!
27 Replies
Jimmacle
Jimmacle14mo ago
iterate over the IEnumerable and .Add each element to the observable collection?
Insire
Insire14mo ago
except that you dont want to load the data from the table asynchronously in the ctor of your viewmodel you dont do async in ctors because its not supported and if you do it anyway, you get fun bugs eventually and you'll only discover them eventually during runtime (or not at all and your users will instead)
EricAwesomeness
EricAwesomenessOP14mo ago
Where would the best place be then? Just create another function in the viewmodel that iterates like Jimmacle was suggesting and call THAT from the constructor? @Insire sorry I hope pinging you isn't a huge bother just not sure if you will see this if I don't
Insire
Insire14mo ago
that other method would need to be async aswell, and you'll be back where you started there is 2 ways to have async code in c# GUIs. async (void) eventhandlers and the entry point of the procss, aka the main method you usually dont have access to the main method which leaves eventhandlers
nohopestage
nohopestage14mo ago
You can also use async commands
Insire
Insire14mo ago
some1 still has to trigger them
EricAwesomeness
EricAwesomenessOP14mo ago
I am learning now about events which is a new topic so it's taking me a bit but this is pointing me in the right direction
nohopestage
nohopestage14mo ago
Yeah well you can do that manually
Insire
Insire14mo ago
yes, from an eventhandler
nohopestage
nohopestage14mo ago
Not necessarily, I usually do it from the factory method of my vms
EricAwesomeness
EricAwesomenessOP14mo ago
So if I'm understanding correctly the class that I load the sql data from should be setup as the publisher and create a delegate there. Then the subscriber classes uses the signature the delegate setup to subscribe to the event so when the event goes off, in this case the event is the player data loads, the subscribers event handler which is on the mainviewmodel can take the event args it gets and use it right?
Insire
Insire14mo ago
wha.. just subscribe to the loaded event of your window or something then start your async method there wrap it in a try catch, call it a day
EricAwesomeness
EricAwesomenessOP14mo ago
Oh wow. That's a simpler then creating my own event. And subscribing to that. Should the async method be on the data class or the mainviewmodel or does it not matter?
Insire
Insire14mo ago
the async method is what is doing async work sometimes thats the viewmodel sometimes thats a view(window or control)
nohopestage
nohopestage14mo ago
@EricAwesomeness I'd create an async command (you could use AsyncRelayCommand from CommunityToolkit.Mvvm), create a static method in the vm class that creates a new instance of it (the vm class), executes the command and returns the instance So the command would await LoadPlayers and assign the result to Players
Insire
Insire14mo ago
... and you'll still be calling that command from an eventhandler, like the loaded event i mentioned before, having a command is useful if you want to bind it to a button or something, otherwise its pointless busywork
nohopestage
nohopestage14mo ago
No, I meant calling it from the factory method
Insire
Insire14mo ago
i dont think eric has a factory method
nohopestage
nohopestage14mo ago
Yeah, I suggested creating it as an option Words are hard
EricAwesomeness
EricAwesomenessOP14mo ago
Sorry im back home and have time to work on this What is a factory method?
nohopestage
nohopestage14mo ago
It creates a new instance of MainViewModel, populates the collection by executing a command and returns the instance
Insire
Insire14mo ago
a method with the purpose of creating (calling a ctor) something
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
EricAwesomeness
EricAwesomenessOP14mo ago
Is this the better way to do it then? In the constructor of main view model i subscribe to the window creation event and then when the event triggers it creates the DataWarehouse class and runs a method from there that returns an observablecollection to the mainviewmodel? https://paste.mod.gg/llcexsgdgdph/0 Im sorry if im still not understanding quite well. But I believe this is what you meant right?
Insire
Insire14mo ago
you can't reference a window in your viewmodel, that breaks MVVM that means your viewmodel knows about the view which is the opposite you try to achieve with MVVM just put this
Activated += (s, e) =>
{
LoadData();
};
}
Activated += (s, e) =>
{
LoadData();
};
}
in your window
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server