在 Xamarin Forms 中添加值后,ListView 不刷新

Atu*_*tul 3 c# xamarin xamarin.forms

我正在做的是通过 2 个以上的页面传递数据。我在导航时将视图模型分配给下一页。在第二页中,我有一个在添加值后不刷新/更新的列表视图。

请帮帮我!!

这是我的代码

我的视图模型

public class MyViewModel : INotifyPropertyChanged
    { 
        public string _userName { get; set; }
        public List<family> familyList;
        public List<family> FamilyList
        {
            get { return familyList; }
            set
            {
                familyList = value;
                OnPropertyChanged();
            }
        }       
        public event PropertyChangedEventHandler PropertyChanged;
        public MyViewModel()
        {
            _userName = "Mak";
            familyList = new List<family>();
        }
        public void AddMember(string memberName)
        {
            FamilyList.Add(new family
            {
                name = memberName,
                id = Guid.NewGuid().ToString(),
                username=_userName
            });
        }
 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
}
Run Code Online (Sandbox Code Playgroud)

用户详细信息.xaml

<cl:BasePage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="familyinfo.userdetails" xmlns:cl="clr-namespace:familyinfo;assembly=familyinfo">
<Label Font="Roboto-Medium" FontSize="14" Text="{Bindinbg _userName}" />
<Button Clicked="Next_Step" HeightRequest="30" HorizontalOptions="FillAndExpand" BorderRadius="12" Text="NEXT" />
</cl:BasePage>
Run Code Online (Sandbox Code Playgroud)

用户详细信息.xaml.cs

public partial class userdetails : BasePage
    {
        public MyViewModel _myViewModel { get; set; }
        public userdetails()
        {
            InitializeComponent();
            BindingContext = new MyViewModel();
        }
        void Next_Step(object sender, System.EventArgs e)
        {
            _myViewModel =(MyViewModel) this.BindingContext;
            var familyMember = new FamilyMember();
            familyMember.BindingContext = _myViewModel;
            Application.Current.MainPage = new NavPage(registerCar);
         }
     }
Run Code Online (Sandbox Code Playgroud)

家庭成员.xaml

 <cl:BasePage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="familyinfo.FamilyMember" xmlns:cl="clr-namespace:familyinfo;assembly=familyinfo">
    <Label Font="Roboto-Medium" FontSize="14" Text="{Bindinbg _userName}" />
<cl:CustomEntry x:Name="txtMemberName" Placeholder="Member Name" FontSize="12" /> 
<Button Clicked="AddMember" HeightRequest="30" HorizontalOptions="FillAndExpand" BorderRadius="12" Text="Add" />
<ListView ItemsSource="{Binding FamilyList}" VerticalOptions="FillAndExpand" BackgroundColor="Transparent">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <Grid Padding="20,10,0,0" ColumnSpacing="12" RowSpacing="0" BackgroundColor="Transparent">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto">
                                        </ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto">
                                        </RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Label Grid.Row="0" Text="{Binding name}" Grid.Column="0" Font="Roboto-Medium" FontSize="14" TextColor="#000000" />
                                </Grid>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    </cl:BasePage>
Run Code Online (Sandbox Code Playgroud)

家庭成员.xaml.cs

public partial class FamilyMember : BasePage
    {
        public MyViewModel _myViewModel { get; set; }
        public userdetails()
        {
            InitializeComponent();
        }
         void AddMember(object sender, System.EventArgs e)
        {
         _myViewModel = (MyViewModel)this.BindingContext;
        _myViewModel.AddMember(txtMemberName.Text);
        }
     }
Run Code Online (Sandbox Code Playgroud)

Fal*_*lko 6

我同意 Atul:使用 anObservableCollection是正确的方法。

一种解决方法 - 如果您没有机会更改它 - 是将ListView's设置ItemSourcenull并返回到列表,每当数据更改且 UI 需要更新时:

void UpdateListView(ListView listView)
{
    var itemsSource = listView.ItemsSource;
    listView.ItemsSource = null;
    listView.ItemsSource = itemsSource;
}
Run Code Online (Sandbox Code Playgroud)


Tai*_*T's 5

事实上,您必须使用实现INotifyCollectionChanged接口的集合(而不是众所周知的INofifyPropertyChanged)。这正是ObservableCollection<T>对你有用的。这就是为什么它像“魔法”一样起作用。