【问题标题】:ListView not refreshing after adding value in Xamarin Forms在 Xamarin Forms 中添加值后 ListView 不刷新
【发布时间】:2017-05-29 03:33:40
【问题描述】:

我正在做的是通过超过 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));
        }
}

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

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

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    我同意 Atul:使用 ObservableCollection 是正确的做法。

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

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

    【讨论】:

    • 感谢@Falko 的宝贵建议。
    • @Atul:如果他们对您的问题有帮助,请随时支持/接受任何给定的答案。这不仅能激励用户写出高质量的答案,还能帮助其他读者快速找出值得尝试的好方法。
    【解决方案2】:

    实际上,您必须使用实现INotifyCollectionChanged 接口的集合(而不是众所周知的INofifyPropertyChanged)。这正是ObservableCollection&lt;T&gt; 对您的作用。这就是为什么它像“魔法”一样工作。

    【讨论】:

    • 同意你的看法。谢谢。
    【解决方案3】:

    我只是使用ObservableCollection 而不是List 并且它有效!!

    【讨论】:

    • 您必须使用可观察对象进行绑定,否则 UI 无法知道发生了什么变化。我建议您查看 MVVM Helpers 库中的 ObservableRangeCollection。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多