Xamarin Forms:创建DataTemplate的实例

Xamarin Forms:创建DataTemplate的实例,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我不想用类型为DataTemplate的BindableProperty创建ContentView,这样当我使用自定义ContentView时,我可以自定义元素的外观 但我不想在代码中安排和创建内容,如何从DataTemplate创建实例 例如,在我的自定义视图中,我有一个对象的集合,现在对于每个对象,我想基于设置的数据模板创建一个视图,并将创建的视图的绑定上下文设置到该对象。我按照以下方式进行了计算 我以以下方式使用自定义的ContentView: <controls:M

我不想用类型为
DataTemplate
BindableProperty
创建
ContentView
,这样当我使用自定义
ContentView
时,我可以自定义元素的外观

但我不想在代码中安排和创建内容,如何从DataTemplate创建实例


例如,在我的自定义视图中,我有一个对象的集合,现在对于每个对象,我想基于设置的数据模板创建一个视图,并将创建的视图的绑定上下文设置到该对象。

我按照以下方式进行了计算

我以以下方式使用自定义的
ContentView

        <controls:MyCustomView Items="{Binding SampleItems}">
            <controls:MyCustomView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding SampleProperty}" />
                </DataTemplate>
            </controls:MyCustomView.ItemTemplate>
        </controls:MyCustomView>
现在让我们假设在我想要创建和排列的
SampleMethodToArrangeItems
方法中,从提供的数据模板创建的项目:

        foreach (var item in Items)
        {
            var itemView = ItemTemplate.CreateContent() as View;
            if (itemView != null)
            {
                itemView.BindingContext = item;
                // Do something with the create view e.g. add it to Grid.Children
            }
        }

我用下面的方法算出了

我以以下方式使用自定义的
ContentView

        <controls:MyCustomView Items="{Binding SampleItems}">
            <controls:MyCustomView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding SampleProperty}" />
                </DataTemplate>
            </controls:MyCustomView.ItemTemplate>
        </controls:MyCustomView>
现在让我们假设在我想要创建和排列的
SampleMethodToArrangeItems
方法中,从提供的数据模板创建的项目:

        foreach (var item in Items)
        {
            var itemView = ItemTemplate.CreateContent() as View;
            if (itemView != null)
            {
                itemView.BindingContext = item;
                // Do something with the create view e.g. add it to Grid.Children
            }
        }

你能发布它的完整工作吗?理解起来有点混乱…提前谢谢你能发布完整的工作渲染器吗?理解起来有点混乱…提前谢谢