Xamarin.Forms-在OnAppearing方法中未更新ListView [英] Xamarin.Forms - ListView isn't being updated in the OnAppearing method

查看:69
本文介绍了Xamarin.Forms-在OnAppearing方法中未更新ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin.Forms开发应用程序,我有一个屏幕,用户可以在其中发布供稿更新,此供稿更新包含文本和一些附件.我的xaml视图基本上是一个StackLayout,其中包含一个ListView,一个Editor和几个Buttons.问题是我试图在onAppearing方法中设置ListViewItemSource.该方法被称为就好了.我正在设置的集合发生了预期的变化.但是由于某些原因,ListView没有被更新.设置Source调用Editor焦点方法后,可以使ListView正常工作的解决方法.我这样做是因为我发现在设置好集合后单击编辑器时,ListView可以正确呈现.但是,这是一个非常讨厌的解决方法.

I'm developing an app with Xamarin.Forms, I've a screen where the user can publish a feed update, this feed update contains text and some attached files. My xaml view is basically, a StackLayout that contains a ListView, and Editor and a couple of Buttons. The problem is that I'm trying to set the ItemSource of my ListView in the onAppearing method. The method is being called just fine. The collection that I'm setting changes as is expected. But for some reason the ListView isn't being updated. A workaround that made the ListView work was right after setting the Source call the Editor focus method. I did because I figured out that when I clicked in the editor after the collection was suppously set, the ListView rendered properly. But well is a very nasty workaround.

这是我的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());
        }
    }

我玩过很多东西,例如:

I played with a lot of things like:

  • 从ListView和包含ListView的StackLayout中更改Horizo​​ntalOptions和VerticalOptions.

  • Changing the HorizontalOptions and the VerticalOptions from the ListView and the StackLayout that contains the ListView.

删除了所有其他控件,除了StackLayout和子ListView.

Removed all other controls except the StackLayout and the child ListView.

但是到目前为止,除了以编程方式调用Editor的Focus方法之外,没有任何作用

But nothing worked so far, except calling the Editor, Focus method programatically

注意:

正如您所看到的,我正在设置的ListView集合是静态的,我直接从另一个页面修改了该集合,但是我认为这是可以的,因为当我在OnAppearing中设置断点时,该集合正在被正确地修改.

As you can see the ListView collection that I'm setting is static, I modify that collection directly from another page, but I think that is OK, because when I set a breakpoint in the OnAppearing the collection was being modify properly.

推荐答案

两种解决刷新问题的方法.

Two ways to fix that refresh issue.

第一种方法

通过重置ItemsSource,强制ListView从您的List<FileObject>集合中刷新:

Force the ListView to refresh from your List<FileObject> collection by resetting the ItemsSource:

当前

protected override void OnAppearing()
{
    AttachedFilesListView.ItemsSource = CurrentSelectedFile;
    UpdateFeedEditor.Focus();
    base.OnAppearing();
}

更改为:

protected override void OnAppearing()
{
    base.OnAppearing();
    AttachedFilesListView.ItemsSource = null;
    AttachedFilesListView.ItemsSource = CurrentSelectedFile;
}

第二种方式

List<FileObject>集合替换为ObservableCollection<FileObject>,以便通过基于NotifyCollectionChangedAction.Add|Remove|...的事件发布更改,这些事件将被ListView监听.

Replace your List<FileObject> collection with an ObservableCollection<FileObject> so changes are published via NotifyCollectionChangedAction.Add|Remove|... based events that the ListView will be listening to.

当前

public static List<FileObject> CurrentSelectedFile = new List<FileObject>();

更改为:

public static readonly ObservableCollection<FileObject> CurrentSelectedFile = new ObservableCollection<FileObject>();

注意:还添加using System.Collections.ObjectModel;

您的OnAppearing变为:

protected override void OnAppearing()
{
    base.OnAppearing();
    AttachedFilesListView.ItemsSource = CurrentSelectedFile;
}

这篇关于Xamarin.Forms-在OnAppearing方法中未更新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆