Listview 表单-列表视图不是';t正在OnAppearing方法中更新
我正在用Xamarin.Forms开发一个应用程序,我有一个屏幕,用户可以在其中发布提要更新,这个提要更新包含文本和一些附加文件。我的xaml视图基本上是一个Listview 表单-列表视图不是';t正在OnAppearing方法中更新,listview,xamarin,xamarin.forms,Listview,Xamarin,Xamarin.forms,我正在用Xamarin.Forms开发一个应用程序,我有一个屏幕,用户可以在其中发布提要更新,这个提要更新包含文本和一些附加文件。我的xaml视图基本上是一个StackLayout,它包含一个ListView、一个编辑器和两个按钮。问题是我试图在onAppearing方法中设置我的ListView的ItemSource。正在调用该方法。我正在设置的集合按预期更改。但是由于某些原因,ListView没有被更新。在设置源代码调用编辑器焦点方法之后,一个使列表视图正常工作的解决方法。我这样做是因为我发
StackLayout
,它包含一个ListView
、一个编辑器和两个按钮。问题是我试图在onAppearing
方法中设置我的ListView
的ItemSource
。正在调用该方法。我正在设置的集合按预期更改。但是由于某些原因,ListView
没有被更新。在设置源代码
调用编辑器
焦点方法之后,一个使列表视图
正常工作的解决方法。我这样做是因为我发现,当我在集合设置完成后单击编辑器时,ListView
正确呈现。但这是一个非常糟糕的解决办法
这是我的xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HalliganTL.View.UpdateFeedPage">
<StackLayout Orientation="Vertical" >
<ListView x:Name="AttachedFilesListView" MinimumHeightRequest="50" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Button Text="[X]" />
<StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
<Label Text="{Binding Name}" VerticalTextAlignment="Center" FontSize="Medium" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Editor x:Name="UpdateFeedEditor" FontSize="15" Text="{Binding PostText}" VerticalOptions="FillAndExpand" />
<Button Text="Attach file" TextColor="White" BackgroundColor="#77D065" Clicked="onAttachFileClicked"/>
<Button Text="Post" TextColor="White" BackgroundColor="#77D065" Clicked="onPostClicked"/>
</StackLayout>
</ContentPage>
这是我的页面代码:
public partial class UpdateFeedPage : ContentPage
{
public static List<FileObject> CurrentSelectedFile = new List<FileObject>();
public static string PostText = "";
public UpdateFeedPage()
{
InitializeComponent();
Title = "New Update";
UpdateFeedEditor.Text = PostText;
}
protected override void OnAppearing()
{
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
async void onPostClicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
async void onAttachFileClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new FilePickerPage());
}
}
public分部类UpdateFeedPage:ContentPage
{
公共静态列表CurrentSelectedFile=新列表();
公共静态字符串PostText=“”;
公共UpdateFeedPage()
{
初始化组件();
Title=“新更新”;
UpdateFeedEditor.Text=PostText;
}
出现时受保护的覆盖无效()
{
AttachedFileListView.ItemsSource=CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
async void onPostClick(对象发送方,事件参数e)
{
等待导航。PopAsync();
}
异步文件已单击(对象发送方,事件参数e)
{
等待Navigation.PushAsync(新的FilePickerPage());
}
}
我玩了很多东西,比如:
- 从ListView和包含ListView的StackLayout更改水平选项和垂直选项
- 删除了除StackLayout和子ListView之外的所有其他控件
但到目前为止,除了通过编程方式调用编辑器Focus方法外,一切都不起作用
注意:
protected override void OnAppearing()
{
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
protected override void OnAppearing()
{
base.OnAppearing();
AttachedFilesListView.ItemsSource = null;
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
}
public static List<FileObject> CurrentSelectedFile = new List<FileObject>();
public static readonly ObservableCollection<FileObject> CurrentSelectedFile = new ObservableCollection<FileObject>();
正如您所看到的,我正在设置的ListView集合是静态的,我直接从另一个页面修改该集合,但我认为这是可以的,因为当我在OnAppearing中设置断点时,集合被正确修改。有两种方法可以解决刷新问题
第一路)
通过重置项资源
,强制列表视图从列表
集合刷新:
当前:
protected override void OnAppearing()
{
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
protected override void OnAppearing()
{
base.OnAppearing();
AttachedFilesListView.ItemsSource = null;
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
}
public static List<FileObject> CurrentSelectedFile = new List<FileObject>();
public static readonly ObservableCollection<FileObject> CurrentSelectedFile = new ObservableCollection<FileObject>();
更改为:
protected override void OnAppearing()
{
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
protected override void OnAppearing()
{
base.OnAppearing();
AttachedFilesListView.ItemsSource = null;
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
}
public static List<FileObject> CurrentSelectedFile = new List<FileObject>();
public static readonly ObservableCollection<FileObject> CurrentSelectedFile = new ObservableCollection<FileObject>();
第二种方式)
将您的列表
集合替换为可观察集合
,以便通过NotifyCollectionChangedAction发布更改。添加|删除|…
基于列表视图
将侦听的事件
当前:
protected override void OnAppearing()
{
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
UpdateFeedEditor.Focus();
base.OnAppearing();
}
protected override void OnAppearing()
{
base.OnAppearing();
AttachedFilesListView.ItemsSource = null;
AttachedFilesListView.ItemsSource = CurrentSelectedFile;
}
public static List<FileObject> CurrentSelectedFile = new List<FileObject>();
public static readonly ObservableCollection<FileObject> CurrentSelectedFile = new ObservableCollection<FileObject>();
当您说“ListView未被更新”
时,是否意味着您已经查看了UpdateFeedPage
,转到其他页面并返回?或者是你第一次查看更新FeedPage时的
,这意味着我已经查看了更新FeedPage.INotifyPropertyChanged,这是一份持续赠送的礼物。太棒了!两种解决方案都非常有效,我显然选择了第二种。