将数据绑定到Xamarin表单中的Scrollview Stacklayout

时间:2019-03-27 15:43:20

标签: c# xamarin.forms binding

我看到了一篇关于xamarin形式的文章,内容带有一些“可绑定的爱” https://blog.xamarin.com/xamarin-forms-3-5-a-little-bindable-love/

我想尝试一下并创建一个滚动列表,但是由于某种原因,我绑定的数据似乎没有出现在视图中。绑定应该可以正常工作,因为在我的XAML中,我绑定了许多其他东西,并且绑定成功,但是由于某种原因,这种特殊绑定无法正常工作。

XAML:

<ScrollView>
   <StackLayout BackgroundColor="Transparent" BindableLayout.ItemsSource="{Binding CategoriesList}"  >
                <BindableLayout.ItemTemplate>
                        <DataTemplate>         
                           <StackLayout Orientation = "Horizontal" HeightRequest="150" WidthRequest="80">

                                <Button Text="{Binding Name}" />

                         </StackLayout>
                       </DataTemplate>
                </BindableLayout.ItemTemplate>          
             </StackLayout>
        </ScrollView>

这是我的列表在VM中的显示方式:

private ObservableCollection<Categories> _Categories;
    public ObservableCollection<Categories> CategoriesList
    {
        get
        {
            return this._Categories;
        }
        set
        {
            if (_Categories != value)
            {
                this._Categories = value;
                RaisePropertyChanged("CategoriesList");
            }
        }
    }

当我为其创建数据时:

        CategoriesList = new ObservableCollection<Categories>();

        CategoriesList.Add(new Categories() { Name = "Data 1", ID = "1" });
        CategoriesList.Add(new Categories() { Name = "Data 2", ID = "2" });
        CategoriesList.Add(new Categories() { Name = "Data 3", ID = "3" });

1 个答案:

答案 0 :(得分:2)

我想您的getter和setter存在问题。更改值后,无法正确更新该值。我已经使用INotifyPropertyChanged做完了,而且工作正常。

MainPage.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"
    xmlns:local="clr-namespace:App"
    x:Class="App.MainPage">
    <ContentPage.Content>
        <ScrollView HorizontalOptions="FillAndExpand">
            <StackLayout
                HorizontalOptions="FillAndExpand"
                BackgroundColor="Transparent"
                BindableLayout.ItemsSource="{Binding CategoriesList}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <StackLayout
                            Orientation="Horizontal"
                            HeightRequest="150"
                            HorizontalOptions="FillAndExpand">
                            <Button
                                Text="{Binding Name}" />
                        </StackLayout>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

MainPage.xaml.cs:

  namespace App
  {
    public partial class MainPage : ContentPage
    {

        public MainPage()
        {
            InitializeComponent();
            BindingContext = new MainPageViewModel();
        }

    }
}

MainPageViewModel:

 public class MainPageViewModel : BaseViewModel
{
    private ObservableCollection<CategoriesModel> _Categories;
    public ObservableCollection<CategoriesModel> CategoriesList
    {
        get
        {
            return this._Categories;
        }
        set
        {
            if (_Categories != value)
            {
                this._Categories = value;
                SetPropertyValue(ref _Categories, value);

            }
        }
    }

    public MainPageViewModel()
    {

        CategoriesList = new ObservableCollection<CategoriesModel>
        {
            new CategoriesModel() { Name = "Data 1", ID = "1" },
            new CategoriesModel() { Name = "Data 2", ID = "2" },
            new CategoriesModel() { Name = "Data 3", ID = "3" },
            new CategoriesModel() { Name = "Data 4", ID = "4" },
            new CategoriesModel() { Name = "Data 5", ID = "5" },
            new CategoriesModel() { Name = "Data 6", ID = "6" },
            new CategoriesModel() { Name = "Data 7", ID = "7" }
        };
    }
}

我的BaseViewModel的INotifyPropertyChanged范围:

public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanges([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected void SetPropertyValue<T>(ref T bakingFiled, T value, [CallerMemberName] string proertyName = null)
    {
        bakingFiled = value;
        OnPropertyChanges(proertyName);

    }

}

所有视图模型类都扩展了BaseViewModel以便访问SetPropertyValue()方法。

输出:

OutPut