Xamarin ListView MVVM DataBinding

时间:2018-03-06 10:31:14

标签: c# xamarin data-binding

我对ListView的DataBinding有一点问题。

因为我想要一个包含MultiSelection的Listview,我需要实现一个名为 GenericSelectableItem 的自定义类,它存储数据,如果单元格 IsSelected

首先,这是MainPage的视图模型:

public class MainPageViewModel : BaseViewModel, INotifyPropertyChanged
{
    private ObservableCollection<GenericSelectableItem<AudioFile>> _audiofiles = new ObservableCollection<GenericSelectableItem<AudioFile>>();

    public ObservableCollection<GenericSelectableItem<AudioFile>> AudioFiles
    {
        get => _audiofiles ?? new ObservableCollection<GenericSelectableItem<AudioFile>>();
        set
        {
            _audiofiles = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(AudioFiles)));
        }
    }
}

MainPage的Xaml:

            <!-- The Content -->
            <ListView x:Name="listView" Grid.Row="1" HasUnevenRows="true" RowHeight="-1" ItemsSource="{Binding AudioFiles}" ItemSelected="ListView_OnItemSelected">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <local:AudioViewCell Audiofile="{Binding Data}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

2个用于制作多选ListView的助手类:

public class GenericSelectableItem<T> : SelectableItem
{
    public GenericSelectableItem(T data)
        : base(data)
    {
    }

    public GenericSelectableItem(T data, bool isSelected)
        : base(data, isSelected)
    {
    }

    // this is safe as we are just returning the base value
    public new T Data
    {
        get => (T)base.Data;
        set => base.Data = value;
    }
}

public class SelectableItem : BindableObject
{
    public static readonly BindableProperty DataProperty =
        BindableProperty.Create(
            nameof(Data),
            typeof(object),
            typeof(SelectableItem),
            (object) null);

    public static readonly BindableProperty IsSelectedProperty =
        BindableProperty.Create(
            nameof(IsSelected),
            typeof(object),
            typeof(SelectableItem),
            (object)false);

    public SelectableItem(object data)
    {
        Data = data;
        IsSelected = false;
    }

    public SelectableItem(object data, bool isSelected)
    {
        Data = data;
        IsSelected = isSelected;
    }

    public object Data
    {
        get => (object)GetValue(DataProperty);
        set => SetValue(DataProperty, value);
    }

    public bool IsSelected
    {
        get => (bool)GetValue(IsSelectedProperty);
        set => SetValue(IsSelectedProperty, value);
    }
}

AudioViewCell.Xaml中的绑定示例:

<Label x:Name="LblFilename" Text="{Binding Filename}"
                               VerticalTextAlignment="Center"
                               Style="{StaticResource CellLabel}"/>

AudioViewCell.cs

public partial class AudioViewCell : ViewCell
{
    public static BindableProperty AudiofileProperty = BindableProperty.Create(
        propertyName: nameof(Audiofile),
        returnType: typeof(AudioFile),
        declaringType: typeof(AudioViewCell),
        defaultValue: null,
        defaultBindingMode: BindingMode.OneWay);

    public AudioFile Audiofile
    {
        get => (AudioFile) GetValue(AudiofileProperty);
        set
        {
            Debug.WriteLine("Audiofile changed");
            SetValue(AudiofileProperty, value);
            ((MenuItemViewModel) BindingContext).Audiofile = value;
        }
    }

    public AudioViewCell()
    {
        InitializeComponent();

        this.BindingContext = new MenuItemViewModel(SlAdditionalData, AwvWaveView);
    }
}

最后是MenuItemViewModel:

public class MenuItemViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private AudioFile _audioFile;
    public AudioFile Audiofile
    {
        get => _audioFile;
        set
        {
            Debug.WriteLine("Setting Audiofile");
            _audioFile = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(Audiofile)));
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(Filename)));
        }
    }

    public string Filename => Audiofile?.Filename;
}

似乎 GenericSelectableItem 中的Field Data 从未设置过,所以我认为绑定有问题

有谁知道更好的方法或为什么这不起作用? 非常感谢你的帮助!!

1 个答案:

答案 0 :(得分:1)

TL; DR版本:深入了解您的cell'cellViewModel'源代码我注意到您的绑定句柄存在混淆码。您正在处理在AudioViewCell的构造函数中设置的一个BindingContext,但它被ListView自动覆盖(在构造函数之后运行)。所以你站在没有数据的情况下渲染的ViewCell。

在此图片上,我试图展示您的模型正在发生什么:

sample Diagram

请注意左边的黄色圆形箭头,它是您在构造函数中定义绑定上下文。之后被红色箭头覆盖(在listview渲染后设置)。

要使其按照您编码的方式运行,请按以下步骤操作:

  1. 摆脱AudiofileViewCell的AudiofileProperty,你不需要它;
  2. MenuItemViewModel的构造函数创建一个重载以接收“AudioFile”(MenuItemViewModel类是您真正的BindingContext);
  3. 覆盖OnBindingContextChanged方法以提取新的Data字段,并将其作为参数发送到MenuItemViewModel的新实例的构造函数;
  4. 将此新MenuItemViewModel实例设置为内部视图的BindingContext(根据您的源代码,它是一个名为slRoot的StackLayout)
  5. 以下是步骤代码:

    <强> 2

    public class MenuItemViewModel : INotifyPropertyChanged
    {
        // ...
        public void SetAudiofile(AudioFile data)
        {
            Audiofile = data;
        }
        // ...
    }
    

    3和4:

    public partial class AudioViewCell : ViewCell
    {
        // ...
        protected override void OnBindingContextChanged()
        {
            base.OnBindingContextChanged();
            // * I'm not sure if it's ok create a new instance to your binding context here, the old one ca be kept on memory due it's subscription. Think about create a method to set just the Audiofile property
            slRoot.BindingContext = new MenuItemViewModel( thing, thing, ((GenericSelectableItem<AudioFile>)BindingContext).Data); 
        }
        // ...
    }
    

    我已经测试过并且有效,但它远非理想的清洁解决方案。

    如果您的意图是重用此单元格,我认为您应该公开可以绑定或不绑定的属性,让它需要显示将要显示的内容。视图单元应该只处理可视布局/行为,无论数据是什么都无关紧要。

    P.S。:对不起我的英语不好,我希望这是可以理解的。