❔ 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
iterate over the IEnumerable and
.Add
each element to the observable collection?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)
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
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
You can also use async commands
some1 still has to trigger them
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
Yeah well you can do that manually
yes, from an eventhandler
Not necessarily, I usually do it from the factory method of my vms
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?
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
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?
the async method is what is doing async work
sometimes thats the viewmodel
sometimes thats a view(window or control)
@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
... 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
No, I meant calling it from the factory method
i dont think eric has a factory method
Yeah, I suggested creating it as an option
Words are hard
Sorry im back home and have time to work on this
What is a factory method?
It creates a new instance of
MainViewModel
, populates the collection by executing a command and returns the instancea method with the purpose of creating (calling a ctor) something
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.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?
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 in your window
https://learn.microsoft.com/en-us/dotnet/api/system.windows.window.activated?view=windowsdesktop-7.0#remarks
Loaded
suits better imoWindow.Activated Event (System.Windows)
Occurs when a window becomes the foreground window.
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.