【问题标题】:Xamarin.Forms DataTemplateSelector doesnt work on iOS (Constructor not called)Xamarin.Forms DataTemplateSelector 在 iOS 上不起作用(未调用构造函数)
【发布时间】:2021-07-19 18:17:47
【问题描述】:

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

<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 上,它工作得很好。

这里有什么问题?

谢谢

【问题讨论】:

标签: ios xamarin.forms


【解决方案1】:

我们经常在 xaml 中定义DataTemplate 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,你可以在数据模板选择器构造函数中创建它们。

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 ;
            });

        }

【讨论】:

  • 即使我这样做了,ios 也不会返回任何东西在选择模板上也永远不会被调用
  • 您介意与我们分享一个基本的、最小的项目进行测试吗?由于它在 side 上运行良好,因此构造函数按预期调用。
  • 我发现了问题:这个:
  • 没关系,如果您有任何其他问题,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2018-12-10
  • 2012-03-12
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多