[英]Binding data to a Scrollview Stacklayout in Xamarin Forms
I saw an article on xamarin forms with some "Bindable love" https://blog.xamarin.com/xamarin-forms-3-5-a-little-bindable-love/ . 我看到了一篇关于xamarin形式的文章,其中包含一些“可绑定的爱” https://blog.xamarin.com/xamarin-forms-3-5-a-little-bindable-love/ 。
I wanted to try it out and create a scrolling list but for some reason, the data that i am binding does not seem to appear in the view. 我想尝试一下并创建一个滚动列表,但是由于某种原因,我绑定的数据似乎没有出现在视图中。 The binding should work fine because in my XAML i bind plenty of other things and it works successfully, but for some reason this particular binding is not working. 绑定应该可以正常工作,因为在我的XAML中,我还绑定了许多其他东西,并且绑定成功,但是由于某种原因,该特定绑定无法正常工作。
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>
This is how my list looks in the VM: 这是我的列表在VM中的外观:
private ObservableCollection<Categories> _Categories;
public ObservableCollection<Categories> CategoriesList
{
get
{
return this._Categories;
}
set
{
if (_Categories != value)
{
this._Categories = value;
RaisePropertyChanged("CategoriesList");
}
}
}
And when i create data to it: 当我创建数据时:
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" });
I guess there is an issue with your getter and setter. 我想您的吸气剂和吸气剂有问题。 It is not properly updating the value once it is changed. 更改值后,无法正确更新该值。 I have done by using INotifyPropertyChanged
and it is working fine. 我已经通过使用INotifyPropertyChanged
完成,并且工作正常。
MainPage.xaml: 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: MainPage.xaml.cs中:
namespace App
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}
}
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" }
};
}
}
My BaseViewModel which extent the INotifyPropertyChanged: 我的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);
}
}
All the view model class extend the BaseViewModel in order to access the SetPropertyValue()
method. 所有视图模型类都扩展了BaseViewModel以便访问SetPropertyValue()
方法。
Output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.