Home > database >  How to call the refresh task on appearing of the V from VM
How to call the refresh task on appearing of the V from VM

Time:03-23

I have some NoteItem objects, which I store in DB. I have edited them in the EditingPage, and they are in DB already, that works. But when I go back to the MainPage, I want to call the Task Refresh() to display the new NoteItem object, which I added to my DB. How can I do it?

Refresh Task from NoteItemsViewModel.cs (used in MainPage)

public AsyncCommand RefreshCommand { get; }
        public async Task Refresh()
        {
            await Task.Delay(2000);
            NoteItems.Clear();
            var NoteItemsBeingRefreshed = await Service.GetNotesFromDB();
            NoteItems.AddRange(NoteItemsBeingRefreshed);

            //for debugging
            foreach (var noteItem in NoteItems)
            {
                Console.WriteLine(noteItem.Id);
                Console.WriteLine(" "   noteItem.Text);
            }

        }

As I see, not all the people understood my problem. First what I want to say, NoteItems is ObservableRangeCollection. Second, the problem was not to display the update, but to update Items in this collection from SQL Database that I have.

MainPage.xaml

<ListView 
                x:Name="NoteItemsList" 
                ItemsSource="{Binding NoteItems}" 
                Margin="0"
                FlexLayout.AlignSelf="Start"
            >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >
                            <FlexLayout JustifyContent="SpaceBetween" AlignItems="Center" Margin="20,0">
                                <Label 
                                    Text="{Binding Text}"
                                />

and what I do in my EditingPage VM to update Database await Service.AddNoteToDB(Text);

CodePudding user response:

In Xamarin, you can use ObservableCollection instead of List for temporary storage of data.The xaml page will automatically refresh every time the value of the ObservableCollection is modified.

For your logic needs, there are two workarounds.

  1. Modify the value inside the ObservableCollection after each modification to the database, so that the value of the xaml page will automatically refresh.

  2. Query the value from the database and change the value of the ObservableCollection every time you jump to the MainPage(override the OnAppearing method).

CodePudding user response:

You do not need to call a refresh method to display the new values to the UI. You should be using an observable range collection that is bound to a data template in your XAML, which is then used to update via bindings to the UI elements you want them bound to. Keep in mind that you do not want to use a foreach loop to update the property. Instead, use a list, then at the end of the foreach loop, set your observable range collection property = to the list. This will prevent the UI from redrawing each iteration of the foreach loop because it will only update the bound property once.

  • Related