Xamarin.Forms DataTemplateSelector在iOS上不起作用(未调用构造函数) [英] Xamarin.Forms DataTemplateSelector doesnt work on iOS (Constructor not called)

查看:51
本文介绍了Xamarin.Forms DataTemplateSelector在iOS上不起作用(未调用构造函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样夸大数据模板选择器:

I am inflating a data template selector like so:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:MyDataTemplateSelector x:Key="templateSelector"></local:MyDataTemplateSelector>
    </ResourceDictionary>
</ContentPage.Resources>

            <Grid Grid.Row="0"   >

                <abstractions:CarouselViewControl VerticalOptions="Start"  x:Name="carouselview" ItemTemplate="{StaticResource templateSelector}" />

            </Grid>

代码如下:

    public MyDataTemplateSelector()
    {
        Layout1 = new DataTemplate(typeof(CV_CarouselView_ShowPics));
        Layout2 = new DataTemplate(typeof(CV_VideoPlayer));
    }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        FullAdType cell = (FullAdType)item;

        if(cell != null && cell.hasVideo )
        {

            return Layout2; // get video layout 

        }

        else
            return Layout1;

    }

但是在iOS上都不会调用任何数据模板选择器构造函数,但在Android上,都可以正常工作.

But neither of the data template selectors constructors is called on iOS but on Android, it works just fine.

这是什么问题?

谢谢

推荐答案

我们经常在xaml中定义 DataTemplate

We often define the DataTemplate in xaml ,check here .

 <ContentPage.Resources>
        <DataTemplate x:Key="AmericanMonkeyTemplate">
            ...
        </DataTemplate>

        <DataTemplate x:Key="OtherMonkeyTemplate">
            ...
        </DataTemplate>

        <controls:MonkeyDataTemplateSelector x:Key="MonkeySelector"
                                             AmericanMonkey="{StaticResource AmericanMonkeyTemplate}"
                                             OtherMonkey="{StaticResource OtherMonkeyTemplate}" />
    </ContentPage.Resources>

    <CarouselView ItemsSource="{Binding Monkeys}"
                  ItemTemplate="{StaticResource MonkeySelector}" />


但是,如果要在后面的代码中初始化DataTemplate,则可以在数据模板选择器构造函数中创建它们.


However , if you want to initialize DataTemplate in code behind, you can create them in data template selector constructor .

public PersonDataTemplateSelector()
        {
            ValidTemplate = new DataTemplate(()=> {

                var grid = new Grid();

                var nameLabel = new Label { FontAttributes = FontAttributes.Bold };
                nameLabel.TextColor = Color.Green;
                nameLabel.SetBinding(Label.TextProperty, "Name");
                grid.Children.Add(nameLabel);

                return grid ;
            });

            InvalidTemplate = new DataTemplate(() => {

                var grid = new Grid();

                var nameLabel = new Label {  };
                nameLabel.TextColor = Color.Red;
                nameLabel.SetBinding(Label.TextProperty, "Name");
                grid.Children.Add(nameLabel);

                return grid ;
            });

        }

这篇关于Xamarin.Forms DataTemplateSelector在iOS上不起作用(未调用构造函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆