我的自定义渲染器有问题。我的示例基于官方Xamarin站点的示例,但是当我将一个Item添加到绑定列表时,我无法触发渲染器中的OnElementPropertyChanged方法。
细胞:
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,但之后似乎没有任何事情发生。如果有人知道原因,我将不胜感激。
答案 0 :(得分:1)
当属性确实发生变化时,会触发PropertyChanged
事件。您的列表不会更改,只会更改其内容,因此具有列表本身的属性仍然引用相同的对象(列表)。
您正在使用ObservableCollection
,这是可行的方法。问题是您必须在渲染器中订阅CollectionChanged
事件。
此外,使用默认ListView
可能更容易,只需为单元格创建自定义渲染器。