qmlnet/qmlnet-examples

Off thread example?

Closed this issue · 1 comments

Not sure if this is the right place for this, my apologies if it is.

I have tried to follow the photoframe example but got a bit lost...

Anyways I was wondering if you could either advise a method or provide a brief example of binding a listview to a .net model class with a list and having a separate thread / Task.Run scope which receives some remote data and loads the results onto the UI.
My need for this is receiving websocket data and displaying the results in the UI and most importantly having the UI update as new data comes in through a socket.

I got a basic version working by using your photoframe sample as inspiration.

Essentially I registered a Singleton class at startup and when it completed loading on the UI I started the websocket in a Task.Run block and stored the results in a class list property.
I can load the data into the UI if I load another page and navigate back to the page which displays the data. But I haven't been able to get the page to reload the UI control on the page as in your collections model example.

Hopefully this is formatted correctly as I'm on my phone.

I would do something like this:

public class QmlObject
{
    private List<int> _items;

    public async Task Load()
    {
        var result = await Task.Run(() =>
        {
            var items = new List<int>();
            // ...long running op to get list of ints.
            return items;
        });
        Items = result;
    }

    public List<int> Items
    {
        get => _items;
        set
        {
            _items = value;
            this.ActivateNotifySignal();
        }
    }
}

Then, bind against items this way.

Repeater {
    model: Net.toListModel(yourObject.items)
}