Xamarin MvvmCross创建到LinearLayout的自定义绑定
我正在尝试创建一个到LinearLayout的自定义绑定,以允许我动态创建一个视图,并将其作为子对象绑定到LinearLayout。对于熟悉WPF的人来说,这类似于ContentControl或从ContentControl派生的任何WPF控件提供的功能。基本上,您可以创建动态内容并绑定到ContentControl的content属性 以下是我为自定义绑定准备的内容:Xamarin MvvmCross创建到LinearLayout的自定义绑定,xamarin,mvvmcross,Xamarin,Mvvmcross,我正在尝试创建一个到LinearLayout的自定义绑定,以允许我动态创建一个视图,并将其作为子对象绑定到LinearLayout。对于熟悉WPF的人来说,这类似于ContentControl或从ContentControl派生的任何WPF控件提供的功能。基本上,您可以创建动态内容并绑定到ContentControl的content属性 以下是我为自定义绑定准备的内容: public class MvxLinearLayoutContentTargetBinding : MvxPropert
public class MvxLinearLayoutContentTargetBinding : MvxPropertyInfoTargetBinding<LinearLayout>
{
public MvxLinearLayoutContentTargetBinding(object target, PropertyInfo targetPropertyInfo) : base(target, targetPropertyInfo)
{
}
protected override void SetValueImpl(object target, object value)
{
base.SetValueImpl(target, value);
var view = target as LinearLayout;
if (view == null) return;
view.AddView((View)value);
}
public override Type TargetType
{
get { return typeof(LinearLayout); }
}
}
自定义绑定也已在Setup.cs中注册,如下所示:
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterPropertyInfoBindingFactory(
typeof(MvxLinearLayoutContentTargetBinding),
typeof(LinearLayout), "Content");
base.FillTargetFactories(registry);
}
然而,所有这些都准备好了,我看不到我的观点。MvvmCross已经支持这个的原始版本。尽管没有DataTemplateSelector 您可以将ViewModels集合绑定到MvxLinearLayout.ItemsSource。您还需要记住设置ItemTemplateId: 但是,这是非常低效的,因为它不回收视图等。因此,如果您需要支持DataTemplateSelector的变体,请改用MvxRecyclerView
public class CustomViewModel : MvxViewModel
{
public object CustomView { get; set; }
}
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterPropertyInfoBindingFactory(
typeof(MvxLinearLayoutContentTargetBinding),
typeof(LinearLayout), "Content");
base.FillTargetFactories(registry);
}
<MvxLinearLayout
...
local:MvxItemTemplateId="@layout/layout_to_repeat"
local:MvxBind="ItemsSource ViewModelCollection"
/>