简体   繁体   English

MVVM具有不同页面xamarin形式的列表数据

[英]MVVM with list data on different page xamarin forms

I have a list of objects that i bind to a ListView on a NavigationPage 我有一个绑定到NavigationPage上ListView的对象列表

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. 使用将更改绑定到模型视图的MVVM方法,我保存更改,然后弹出详细信息导航页面。 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()? 还是我通过重写OnAppering()来手动重新加载模型视图?

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. 在没有看到任何其他代码的情况下,我将假定一旦将Log模型传递到Log Details页面中,该模型就不再与上一页中包含的Log项目集合相关联。

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() . 正如您所描述的,最简单的一种方法是刷新OnAppearing()的Log集合。

Another method I use is a MessagingCenter subscription that you subscribe to in the constructor of your Log Collection page and never unsubscribe from. 我使用的另一种方法是您在Log Collection页面的构造函数中订阅的MessagingCenter订阅,从不取消订阅。 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. 如果有时甚至在OnAppearing()可能不会再次运行时,或者在Android导致简单页面事件陷入OnAppearing()时, OnAppearing()需要刷新集合,则这很好。 >:( > :(

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. 还有一种方法是在某个地方定义一个static Log集合,将其绑定到您的Log Collection页面,然后对单个日志的任何更改都会更新该static集合,因此该static集合将始终是最新的。无论在哪个页面上进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM