我正在尝试使用 Xamarin 社区工具包中的C# 标记扩展来创建 Xamarin.Forms 视图,但想使用 MvvmCross 来绑定视图。
我有一个 MvxContentPage 的工作解决方案,但我不确定每次设置 ViewModel 时调用 Bind 是正确的方法。有更好的地方吗?
这是我的“工作”内容页面
[MvxTabbedPagePresentation(WrapInNavigationPage = true, Title = "Equipment")]
public sealed class EquipmentPage : MvxContentPage<EquipmentViewModel>
{
private ListView _listView;
public EquipmentPage()
{
Build();
}
protected override void OnViewModelSet()
{
base.OnViewModelSet();
Bind();
}
private void Build()
{
//On<iOS>().SetLargeTitleDisplay(LargeTitleDisplayMode.Always);
// Xamarin.Forms.DebugRainbows.DebugRainbow.SetShowColors(this, true);
Visual = VisualMarker.Material;
Content = EquipmentList.Assign(out _listView);
}
private void Bind()
{
var set = CreateBindingSet();
set.Bind(_listView).For(v => v.IsRefreshing)
.To(vm => vm.IsRefreshing);
set.Bind(_listView).For(v => v.RefreshCommand)
.To(nameof(ViewModel.LoadData));
set.Bind(_listView).For(v => v.ItemsSource)
.To(vm => vm.Equipment);
set.Apply();
}
private ListView EquipmentList => new ListView(ListViewCachingStrategy.RecycleElement)
{
// RowHeight = 100,
HasUnevenRows = true,
IsPullToRefreshEnabled = true,
ItemTemplate = new DataTemplate(() => new EquipmentViewCell())
};
}
下一步是为 ListView 绑定一个 ViewCell。我还没有弄清楚如何使这个工作。bindingContext 必须是 IMvxViewModel 吗?
这是我当前的 ViewCell
public class EquipmentViewCell : MvxViewCell
{
private Label _nameLabel, _modelLabel, _typeLabel;
public EquipmentViewCell()
{
Build();
}
enum EquipmentRow
{
Top,
Bottom
}
enum EquipmentColumn
{
Left,
Right
}
private void Build()
{
View = new Grid
{
RowDefinitions = GridRowsColumns.Rows.Define(
(EquipmentRow.Top, GridLength.Auto),
(EquipmentRow.Bottom, GridLength.Auto)),
ColumnDefinitions = GridRowsColumns.Columns.Define(
(EquipmentColumn.Left, GridLength.Star),
(EquipmentColumn.Right, GridLength.Auto)),
Children =
{
new StackLayout
{
Orientation = StackOrientation.Horizontal,
Children =
{
new Label
{
LineBreakMode = LineBreakMode.TailTruncation
}
.FontSize(20).Bold()
.StartExpand()
.Bind(nameof(EquipmentDto.Name))
.Assign(out _nameLabel),
new PancakeView
{
CornerRadius = new CornerRadius(8),
Content = new Label
{
TextColor = Color.White,
LineBreakMode = LineBreakMode.TailTruncation
}.FontSize(16)
.Bind(convert: (EquipmentDto x) =>
{
var converter = new EquipmentStatusValueConverter();
return converter.Convert(x, typeof(string), null, null);
})
}
.Padding(new Thickness(8, 2, 8, 2))
.End()
.Bind(VisualElement.BackgroundColorProperty, convert: (EquipmentDto x) =>
{
var converter = new EquipmentStatusColorValueConverter();
return converter.Convert(x, typeof(Color), null, null);
}),
}
}.Row(EquipmentRow.Top).ColumnSpan(GridRowsColumns.All<EquipmentColumn>()),
new Label
{
LineBreakMode = LineBreakMode.TailTruncation,
TextColor = ColorPalette.SecondaryText
}.FontSize(16)
.Row(EquipmentRow.Bottom).Column(EquipmentColumn.Left)
.Bind(nameof(EquipmentDto.Model))
.Assign(out _modelLabel),
new Label
{
LineBreakMode = LineBreakMode.TailTruncation,
HorizontalTextAlignment = TextAlignment.End,
TextColor = ColorPalette.SecondaryText
}.FontSize(16)
.Row(EquipmentRow.Bottom).Column(EquipmentColumn.Right)
.Bind(nameof(EquipmentDto.Type.Name))
.Assign(out _typeLabel),
}
}.Padding(new Thickness(16, 8, 16, 8));
}
private void Bind()
{
var set = this.CreateBindingSet<EquipmentViewCell, EquipmentDto>();
set.Bind(_nameLabel).To(vm => vm.Name);
set.Bind(_typeLabel).To(vm => vm.Type.Name);
set.Bind(_modelLabel).To(vm => vm.Type);
set.Apply();
}
}
我是否在正确的时间绑定?如何绑定 ViewCells?