在viewmodel中不推荐使用绑定

时间:2018-02-08 14:27:48

标签: c# xamarin data-binding xamarin.forms

研究员。 Property.Created的旧方法已弃用,我现在需要使用Property.Created(),但我无法将此类更新为新标准。我试图检查论坛,官方帖子,但我无法看到如何将此代码转换为新标准。

public ImageGallery()
{
    this.Orientation = ScrollOrientation.Horizontal;

    _imageStack = new StackLayout
    {
        Orientation = StackOrientation.Horizontal
    };

    this.Content = _imageStack;
}

public IList<View> Children
{
    get
    {
        return _imageStack.Children;
    }
}

已弃用

public static BindableProperty ItemsSourcePrsoperty =  BindableProperty.Create<ImageGallery, IList>(
    view => view.ItemsSource,
    default(IList),
    BindingMode.TwoWay,
    propertyChanging: (bindableObject, oldValue, newValue) => {
        ((ImageGallery) bindableObject).ItemsSourceChanging();
    },
    propertyChanged: (bindableObject, oldValue, newValue) => {
        ((ImageGallery) bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
    }
);

这是我试图升级它的方式

public static BindableProperty ItemsSourcePrsoperty = BindableProperty.Create<ImageGallery, IList>(
    view => view.ItemsSource,
    default(IList),
    BindingMode.TwoWay,
    propertyChanging: (bindableObject, oldValue, newValue) =>
    {
        ((ImageGallery)bindableObject).ItemsSourceChanging();
    },
    propertyChanged: (bindableObject, oldValue, newValue) =>
    {
        ((ImageGallery)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
    }
);

然而,为此收到了大量的编译错误。 &#34;无法将lambda表达式转换为&#39; string&#39;因为它不是代表类型&#34;

public IList ItemsSource
{
    get
    {
        return (IList)GetValue(ItemsSourceProperty);
    }
    set
    {

        SetValue(ItemsSourceProperty, value);
    }
}

void ItemsSourceChanging()
{
    if (ItemsSource == null)
        return;
}


void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
    if (ItemsSource == null)
        return;

    var notifyCollection = newValue as INotifyCollectionChanged;
    if (notifyCollection != null)
    {
        notifyCollection.CollectionChanged += (sender, args) =>
        {
            if (args.NewItems != null)
            {
                if (args.NewItems.Count > 0)
                {
                    foreach (var newItem in args.NewItems)
                    {
                        var view = (View)ItemTemplate.CreateContent();
                        var bindableObject = view as BindableObject;
                        if (bindableObject != null)
                            bindableObject.BindingContext = newItem;
                        _imageStack.Children.Add(view);
                    }
                }
            }
            else
            {
                _imageStack.Children.Clear();
                foreach (var Item in ItemsSource)
                {
                    var view = (View)ItemTemplate.CreateContent();
                    var bindableObject = view as BindableObject;
                    if (bindableObject != null)
                        bindableObject.BindingContext = Item;
                    _imageStack.Children.Add(view);
                }
            }
        };

已过时

public static readonly BindableProperty SelectedItemProperty =
    BindableProperty.Create<ImageGallery, object>(
        view => view.SelectedItem,
        null,
        BindingMode.TwoWay,
        propertyChanged: (bindable, oldValue, newValue) =>
        {
            ((ImageGallery)bindable).UpdateSelectedIndex();
        }
    );

不再支持弃用/泛型

public static readonly BindableProperty SelectedIndexProperty =
    BindableProperty.Create<ImageGallery, int>(
        carousel => carousel.SelectedIndex,
        0,
        BindingMode.TwoWay,
        propertyChanged: (bindable, oldValue, newValue) =>
        {
            ((ImageGallery)bindable).UpdateSelectedItem();
        }
    );

1 个答案:

答案 0 :(得分:3)

您应该使用其他重载。例如:

public class ImageGallery : ScrollView
{
    public static BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(ImageGallery), BindingMode.TwoWay,
        propertyChanging: (bindableObject, oldValue, newValue) =>
        {
            ((ImageGallery)bindableObject).ItemsSourceChanging();
        },
        propertyChanged: (bindableObject, oldValue, newValue) =>
        {
            ((ImageGallery)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
        }
    );

    public IList ItemsSource
    {
        get { return (IList)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    void ItemsSourceChanging()
    {
    }

    void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
    {
    }
}