C# 在Xamarin表单中添加值后未刷新ListView
我所做的是通过两个以上的页面传递数据。导航时,我将viewmodel指定给下一页。在第二个页面中,我有一个listview,它在添加值后不会刷新/更新 请帮帮我 这是我的密码 MyViewModelC# 在Xamarin表单中添加值后未刷新ListView,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我所做的是通过两个以上的页面传递数据。导航时,我将viewmodel指定给下一页。在第二个页面中,我有一个listview,它在添加值后不会刷新/更新 请帮帮我 这是我的密码 MyViewModel public class MyViewModel : INotifyPropertyChanged { public string _userName { get; set; } public List<family> familyList;
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));
}
}
FamilyMember.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>
<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>
我只是用了
observateCollection
而不是List
,它很管用 我同意Atul的观点:使用可观察收集是正确的方法
如果您没有机会更改,解决方法是将ListView
的ItemSource
设置为null
,并在数据更改和UI需要更新时返回列表:
void UpdateListView(ListView listView)
{
var itemsSource = listView.ItemsSource;
listView.ItemsSource = null;
listView.ItemsSource = itemsSource;
}
事实上,您必须使用实现INotifyCollectionChanged
接口的集合(而不是众所周知的INofifyPropertyChanged
)。这正是observateCollection
为您所做的。这就是为什么它像“魔术”一样工作。您必须使用可观察对象进行绑定,否则UI无法知道发生了什么变化。我建议你看看图书馆里的ObserverAngeCollection。谢谢@Falko给你的宝贵建议。@Atul:如果给出的答案对你的问题有帮助,请随意投票/接受。这不仅能激励用户写出高质量的答案,还能帮助其他读者快速找到值得尝试的好方法。同意你的观点。谢谢
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);
}
}
void UpdateListView(ListView listView)
{
var itemsSource = listView.ItemsSource;
listView.ItemsSource = null;
listView.ItemsSource = itemsSource;
}