0
私はXamarin.Formアプリケーションを開発するためにPrismを使用しています。私は、カスタムカルーセルコントロールを作成しています:ViewModelのリストをカスタムコントロールにバインドするにはどうすればいいですか?
public class CarouselLayout : ScrollView
{
public IList ItemsSource
{
get
{
return (IList)GetValue(ItemsSourceProperty);
}
set
{
SetValue(ItemsSourceProperty, value);
}
}
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create(
nameof(ItemsSource),
typeof(IList),
typeof(CarouselLayout),
null,
propertyChanged: (bindableObject, oldValue, newValue) =>
{
((CarouselLayout)bindableObject).ItemsSourceChanged();
}
);
void ItemsSourceChanged()
{
_stack.Children.Clear();
foreach (var item in ItemsSource) // ItemSource is a list of ViewModels
{
var view = ?; // How to resolve the View for this ViewModel?
var bindableObject = view as BindableObject;
if (bindableObject != null)
bindableObject.BindingContext = item; // Is this the Prism way to do this?
_stack.Children.Add(view);
}
}
}
これはContentPage
のViewModelにあるpublic class MainPageViewModel: BindableBase
{
private List<BaseViewModel> _viewModels;
public List<BaseViewModel> ViewModels
{
get { return _viewModels; }
set
{
SetProperty(ref _viewModels, value);
OnPropertyChanged();
}
}
public MainPageViewModel()
{
ViewModels = CreateViewModelsList();
}
private List<BaseViewModel> CreateViewModelsList()
{
return new List<BaseViewModel>{
new FirstViewModel(),
new SecondViewModel()
};
}
}
をこれは私が私のメインページにカルーセルを作成してのviewmodelsリストを結合する方法である:
var carousel = new CarouselLayout();
[...]
carousel.SetBinding(CarouselLayout.ItemsSourceProperty, "ViewModels");
ViewModelをCarouselLayoutのメソッドでバインドする方法ItemsSourceChanged()
?