0
votes

I have a list of objects that i bind to a ListView on a NavigationPage

Heres the View:

        <ListView  HasUnevenRows="true" x:Name="note_list" HorizontalOptions="FillAndExpand" HeightRequest="50" ItemSelected="note_clicked">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Margin="10, 0, 0, 0" HorizontalOptions="FillAndExpand" Orientation="Vertical" VerticalOptions="Fill">
                            <StackLayout Margin="10 ,10, 10, 0" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                                <Image IsVisible="{Binding responseRequired, Mode=TwoWay}" Source="icons/blue/warning" WidthRequest="20" HorizontalOptions="Start" Margin="0, 0, 0, 0"></Image>
                                <Label HorizontalOptions="Start" Margin="0, 0, 0, 0" FontSize="18" VerticalOptions="CenterAndExpand" Text="{Binding userId , Mode=TwoWay}"></Label>
                                <Label HorizontalTextAlignment="End" VerticalOptions="Center" HorizontalOptions="EndAndExpand" TextColor="#c1c1c1" FontSize="14" Text="{Binding daysFromPost , Mode=TwoWay}"></Label>
                            </StackLayout>
                            <StackLayout HorizontalOptions="Fill" Orientation="Vertical" Margin="10, -5, 10, 10">
                                <StackLayout  HorizontalOptions="StartAndExpand" Orientation="Horizontal" Margin="0, -2, 0, 0">
                                    <Label FontSize="14" TextColor="#c1c1c1" Text="to: "></Label>
                                    <Label  HorizontalOptions="StartAndExpand" FontSize="14" TextColor="#c1c1c1" Text="{Binding noteToo, Mode=TwoWay}"></Label>
                                </StackLayout>
                                <Label FontSize="14" TextColor="Gray" Text="{Binding note, Mode=TwoWay}"></Label>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Imagine when I click on a Log, it goes to another Page with the Log details, and options to alter that Log. Using MVVM method of binding changes to the Model View, I save the changes then pop the details navigation page. But when the list of Logs shows back up the changes are not visible until the Page is reloaded for some reason. Shouldn't Two Way Binding cause the changes to be pushed to the View? Or am I left reloading the Model View manually by overriding OnAppering()?

1
Can you share how you are binding your ListView to a collection? Also, show your code for whatever object is the BindingContext of these cells. It would be the class that has responseRequired as a property. - therealjohn

1 Answers

1
votes

Without seeing any other code, I will assume that once you pass your Log model into your Log Details page, that model is no longer associated with the collection of Log items that is contained in the previous page.

There are a few ways to get around this. Let me know if you would like to see code for any of the ways below.

One way that is the simplest, as you describe, is to refresh the Log collection in OnAppearing().

Another method I use is a MessagingCenter subscription that you subscribe to in the constructor of your Log Collection page and never unsubscribe from. Then you would send an event from your Log Details page which would trigger the collection to be refreshed. This is nice if, at times, you need the collection to be refreshed even when OnAppearing() might not run again or if you run into issues with Android bludgeoning simple page events to death. >:(

Yet another way would be to have a static Log collection defined somewhere, which your Log Collection Page would bind to, and then any change to a single log would update that static collection, so the static collection would always be up-to-date no matter what page does the changes.