c# - 在 Xamarin Forms 中添加值后 ListView 不刷新

标签 c# xamarin xamarin.forms

我正在做的是通过超过 2 个页面传递数据。我在导航时将 View 模型分配给下一页。在第二页中,我有一个 ListView 在添加值后没有刷新/更新。

请帮帮我!!

这是我的代码

我的 View 模型

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));
        }
}

userdetails.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>

userdetails.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);
         }
     }

家庭成员.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>

FamilyMember.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);
        }
     }

最佳答案

我同意 Atul 的观点:使用 ObservableCollection 是正确的方法。

解决方法 - 如果您没有机会更改它 - 是将 ListViewItemSource 设置为 null 并且返回列表,每当数据更改并且 UI 需要更新时:

void UpdateListView(ListView listView)
{
    var itemsSource = listView.ItemsSource;
    listView.ItemsSource = null;
    listView.ItemsSource = itemsSource;
}

关于c# - 在 Xamarin Forms 中添加值后 ListView 不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224615/

相关文章:

c# - 排序和 IComparable 的问题

xamarin - Xamarin iOS 构建的 DevOps CI 错误 在钥匙串(keychain)中找不到有效的 iOS 代码签名 key

c# - Xamarin 表单 DisplayAlert 在从委托(delegate)调用的函数调用时不显示

c# - Xamarin Forms - 换行按钮文本

android - 如何在 Redmi note 8 设备中禁用 Xamarin 表单条目控制的复制/粘贴选项?

c# - 无法在 arraylist 的对象上调用我的方法

c# - 将自定义命名空间添加到 WCF 中的 soap 信封

xaml - 当内容不可见时隐藏列或行

c# - SwitchToThread 与 sleep (1)

xamarin - 如何在可滚动的内部使用 StackLayout 制作 ContentPage?