微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以直接在Xamarin.Forms中使用ListView的TemplatedItems来更新属性吗?

如何解决我可以直接在Xamarin.Forms中使用ListView的TemplatedItems来更新属性吗?

由于某些特殊原因,我无法使用Xamarin.Forms Data Binding,因此我必须直接更新ListView中ItemTemplate创建的视图的属性

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="TestListView.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Grid>
        <ListView x:Name="TestListView" />
        <Button
            Clicked="Button_Clicked"
            HorizontalOptions="Center"
            Text="Change last appearing text"
            VerticalOptions="Center" />
    </Grid>
</ContentPage>

MainPage.xaml.cs

using System.Linq;
using Xamarin.Forms;

namespace TestListView {
    public partial class MainPage : ContentPage {
        private int lastApprearingItemIndex = -1;
        public MainPage() {
            InitializeComponent();
            TestListView.ItemTemplate = new DataTemplate(() => {
                return (new ViewCell() {
                    View = new Label()
                });
            });
            TestListView.ItemsSource = Enumerable.Range(1,10000).ToArray();
            TestListView.ItemAppearing += TestListView_ItemAppearing;
        }
        private void TestListView_ItemAppearing(object sender,ItemVisibilityEventArgs e) {
            lastApprearingItemIndex = e.ItemIndex;
            ((Label)(((ViewCell)(TestListView.TemplatedItems[e.ItemIndex])).View)).Text = "test text for id " + (int)(e.Item);
        }
        private void Button_Clicked(object sender,System.EventArgs e) {
            ((Label)(((ViewCell)(TestListView.TemplatedItems[lastApprearingItemIndex])).View)).Text = "(changed)test text for id " + lastApprearingItemIndex;
        }
    }
}

此测试在android上运行良好

enter image description here

enter image description here

但是在UWP中它什么也没显示

enter image description here

它在WPF中也运行良好。

所以我有两个问题:

1,为什么它不能在UWP中工作?

2,ListView会在Android中重用ListAdapter之类的项目视图吗?

解决方法

1,为什么它不能在UWP中工作?

因为您将ViewCell用于列表视图,并且未将任何值绑定到该列表视图。

如果您坚持不使用数据绑定,则可以像以下格式一样更改后台代码。

 public partial class MainPage : ContentPage
    {
       
        ObservableCollection<string> employees;
        public MainPage()
        {
            InitializeComponent();
           
            employees = new ObservableCollection<string>();
            for (int i = 0; i < 100; i++)
            {
                employees.Add("test text for id " + i);
            }


            TestListView.ItemsSource = employees;
       
        }
        protected override void OnAppearing()
        {
           
            base.OnAppearing();
           
        }
      
        private void Button_Clicked(object sender,System.EventArgs e)
        {
            //set the id that you want to change
            employees[1] = "test111";
         
        }
    }

此处正在运行gif。 enter image description here

2,ListView会在Android中重用ListAdapter之类的项目视图吗?

在xamarin形式中,没有ListAdapter,因此我强烈建议您使用数据绑定,代码将更加优雅和容易。这是一篇很有帮助的文章。

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。