c# - ObservableCollection 不刷新 UI
问题描述
我是 Xamarin 的新手,在 Listview 中刷新数据时遇到问题。我有一些数据和模式的页面,我可以在其中修改它们的值。通过 MessagingCenter 与第一页进行模态通信
我的代码的一些片段:
页:
<ListView ItemsSource="{Binding RecordsList}" HasUnevenRows="True" Grid.Row="1">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid VerticalOptions="StartAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Label Text="{Binding Name}" Grid.Column="0" Grid.Row="0" VerticalTextAlignment="Center"/>
<Label Text="{Binding Result}" Grid.Column="1" Grid.Row="0" VerticalTextAlignment="Center" HorizontalTextAlignment="End" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
视图模型:
private ObservableCollection<Record> recordsList { get; set; }
public ObservableCollection<Record> RecordsList
{
get { return recordsList; }
set
{
recordsList = value;
NotifyPropertyChanged("RecordsList");
}
}
MessagingCenter.Subscribe<ObservableCollection<Record>>(this, "update", (records) =>
{
RecordsList = records;
});
我可以修改数据的模式:
<ListView ItemsSource="{Binding RecordsList, Mode=TwoWay}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid VerticalOptions="StartAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<Label Text="{Binding Name}" Grid.Row="0" VerticalTextAlignment="Center"/>
<Entry Text="{Binding Result}" Keyboard="Numeric" Grid.Row="1" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout>
<Button Text="Zapisz" Command="{Binding SaveRecordsCommand}"/>
</StackLayout>
模态视图模型:
private readonly INavigation _navigation;
public ObservableCollection<Record> RecordsList { get; set; }
public ICommand CloseModalCommand { get; private set; }
public ICommand SaveRecordsCommand { get; private set; }
public EditRecordsViewModel(INavigation navigation, ObservableCollection<Record> recordsList)
{
_navigation = navigation;
RecordsList = recordsList;
CloseModalCommand = new Command(async avatar => await CloseModal());
SaveRecordsCommand = new Command(async avatar => await SaveRecords());
}
private async Task CloseModal()
=> await _navigation.PopModalAsync();
private async Task SaveRecords()
{
ProfilRepository.UpdateRecords(RecordsList);
MessagingCenter.Send<ObservableCollection<Record>>(RecordsList, "update");
await CloseModal();
}
在调试期间 RecordList 被更改但视图没有刷新,这段代码有什么问题?
解决方案
如果您设置了正确的绑定,它应该可以工作,我为您编写了一个演示,您可以检查您的代码是否有任何差异以找出问题:
在 xaml 中,与您的代码相同:
<ListView ItemsSource="{Binding RecordsList}" HasUnevenRows="True" Grid.Row="1">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid VerticalOptions="StartAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Label Text="{Binding Name}" Grid.Column="0" Grid.Row="0" VerticalTextAlignment="Center"/>
<Button Clicked="Button_Clicked" Text="click to change" Grid.Column="1" Grid.Row="0" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在后面的代码中:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
ProfilViewModel model = new ProfilViewModel();
BindingContext = model;
}
private void Button_Clicked(object sender, EventArgs e)
{
var RecordsList = new ObservableCollection<Record>();
RecordsList.Add(new Record { Name = "e" });
RecordsList.Add(new Record { Name = "f" });
RecordsList.Add(new Record { Name = "g" });
MessagingCenter.Send<ObservableCollection<Record>>(RecordsList, "update");
}
}
public class ProfilViewModel : INotifyPropertyChanged
{
private ObservableCollection<Record> recordsList { get; set; }
public ObservableCollection<Record> RecordsList
{
get { return recordsList; }
set
{
recordsList = value;
OnPropertyChanged("RecordsList");
}
}
public ProfilViewModel() {
recordsList = new ObservableCollection<Record>();
recordsList.Add(new Record { Name = "a" });
recordsList.Add(new Record { Name = "b" });
recordsList.Add(new Record { Name = "c" });
MessagingCenter.Subscribe<ObservableCollection<Record>>(this, "update", (records) =>
{
RecordsList = records;
});
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Record : INotifyPropertyChanged
{
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
string name { get; set; }
public string Name
{
set
{
if (name != value)
{
name = value;
OnPropertyChanged("Name");
}
}
get
{
return name;
}
}
}
我认为您应该检查的几点是:
- RecordsList 中的值在您发送
MessagingCenter.Send
. 你可以在里面添加一个断点MessagingCenter.Subscribe
来检查。 - 你在哪里放的
MessagingCenter.Subscribe
,和我里面的代码一样public ProfilViewModel(){}
? - 你的绑定对吗?你
INotifyPropertyChanged
在Record
模型中实现了吗?
推荐阅读
- scrollview - 在 SwiftUI 中,NavigationBarItem 在由嵌入在 List 或 Form 内的 ScrollView 中的 NavigationLink 推送的 View 中不起作用?
- javascript - 在 Webpack4 中设置 ./src 的路径?
- laravel - 为什么我在 Laravel 中得到一个未定义的常量错误?
- javascript - ExpressJs 在 Vanilla jS 上路由和显示页面
- gnuplot - 在 Gnuplot 输出中压缩的图例、标题和轴标签
- excel - 在数据库/Microsoft Access 中创建日期范围
- c# - Thread.Sleep(0) 与 Thread.Sleep(1) 与 Thread.Sleep(2)
- graphql - 如何:具有动态区域/组件的 Strapi GraphQL 突变
- reactjs - 如何为 react 和 jest 测试框架配置 .babelrc?
- python - Django:提交表单后,浏览按钮消失