Xamarin渲染器不响应更改的属性

本文关键字:属性 不响应 Xamarin | 更新日期: 2023-09-27 18:05:18

我有一些自定义渲染器的问题。我的示例是基于来自官方Xamarin网站的示例,但我无法获得渲染器中的OnElementPropertyChanged方法,当我将Item添加到绑定列表时不会触发。

细胞:

public class NativeiOSListViewCell : UITableViewCell
{
    UILabel name;
    public NativeiOSListViewCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
    {
        SelectionStyle = UITableViewCellSelectionStyle.Gray;
        ContentView.BackgroundColor = UIColor.FromRGB(218, 255, 127);
        name = new UILabel()
        {
            Font = UIFont.FromName("Cochin-BoldItalic", 22f),
            TextColor = UIColor.FromRGB(127, 51, 0),
            BackgroundColor = UIColor.Clear
        };
        ContentView.Add(name);
    }
    public void UpdateCell(string caption)
    {
        name.Text = caption;
    }
    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        name.Frame = new CoreGraphics.CGRect(5, 4, ContentView.Bounds.Width - 63, 25);
    }
}

渲染器

[assembly: ExportRenderer(typeof(MyNativeListView), typeof(Blabla.iOS.NativeiOSListViewRenderer))]
namespace Blabla.iOS
{
    public class NativeiOSListViewRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null)
            {
                // Unsubscribe
            }
            if (e.NewElement != null)
            {
                Control.Source = new NativeiOSListViewSource(e.NewElement as MyNativeListView);
            }
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == MyNativeListView.ItemsProperty.PropertyName) //Only triggered for stuff like Height, Width and not Items
            {
                Control.Source = new NativeiOSListViewSource(Element as MyNativeListView);
            }
        }
    }
}

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="Blabla.PlayGroundPage" xmlns:local="clr-namespace:Blabla;assembly=Blabla">
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:Inverter x:Key="inverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout>
            <local:MyNativeListView x:Name="nativeListView" VerticalOptions="FillAndExpand" Items="{Binding LocalItems}" />
            <Button Text="add" Command="{Binding Add}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

背后的代码:

public partial class PlayGroundPage : ContentPage
{
    public PlayGroundPage()
    {
        InitializeComponent();
        PlayGroundViewModel viewModel = new PlayGroundViewModel();
        BindingContext = viewModel;
    }
}

视图模型

public class PlayGroundViewModel : INotifyPropertyChanged
{
    public ICommand Add { get; private set; }
    private ObservableCollection<ListItem> _localItems;
    public ObservableCollection<ListItem> LocalItems { get { return _localItems; } set { _localItems = value; SetChangedProperty("LocalItems"); } }
    public PlayGroundViewModel()
    {
        Add = new Command(() => { AddItem(); });
        LocalItems = new ObservableCollection<ListItem>();
    }
    private void AddItem()
    {
        ListItem item = new ListItem("a", "b", true); //Just to get something to pop up in the list.
        LocalItems.Add(item);
        SetChangedProperty("LocalItems");
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void SetChangedProperty(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

public class ListItem
{
    public string SelectedType { get; set; }
    public string SelectedOption { get; set; }
    public bool IsChecked { get; set; }
    public ListItem(string selectedType, string selectedOption, bool isChecked)
    {
        SelectedType = selectedType;
        SelectedOption = selectedOption;
        IsChecked = isChecked;
    }
}

我已经验证了SetChangedProperty被触发,但之后似乎没有发生任何事情。如果有人能告诉我原因,我将不胜感激。

Xamarin渲染器不响应更改的属性

当属性确实发生变化时触发PropertyChanged事件。你的列表没有改变,只有它的内容改变了,所以列表本身的属性仍然指向同一个对象(列表)。

您正在使用ObservableCollection,这是正确的方法。问题是你必须在渲染器中订阅CollectionChanged事件。

另外,使用默认的ListView并为单元格创建一个自定义渲染器可能更容易。