Xamarin - 加载 CollectionView 时指定的转换无效。页面无法加载,然后应用程序崩溃

问题描述 投票:0回答:1

我有一个集合视图,它通常应该加载所有对象属性,但打开页面时会崩溃:

public void Bind(object itemBindingContext, ItemsView itemsView,
           Action<Size> reportMeasure = null, Size? size = null)
       {
           var template = _template.SelectDataTemplate(itemBindingContext, itemsView);

           var templateChanging = template != _selectedTemplate;

           if (templateChanging)
           {
               // Clean up any content we're still holding on to
               _itemContentView.Recycle();

               // Create the new content
               View = (View)template.CreateContent();

               // Set the binding context _before_ we create the renderer; that way, the bound data will be 
               // available during OnElementChanged
               View.BindingContext = itemBindingContext;

               // Make sure the Visual property is available when the renderer is created
               PropertyPropagationExtensions.PropagatePropertyChanged(null, View, itemsView);

               // Actually create the native renderer
               _itemContentView.RealizeContent(View);

               _selectedTemplate = template;
           }

           _itemContentView.HandleItemSizingStrategy(reportMeasure, size);

           if (!templateChanging)
           {
               // Same template, new data
               View.BindingContext = itemBindingContext;
           }

           itemsView.AddLogicalChild(View);
       }

它在这一行崩溃:

View = (View)template.CreateContent();

这是集合视图:

<CollectionView VerticalScrollBarVisibility="Always"
                                Margin="0,10,20,0" 
                                ItemsSource="{Binding Libraries}"
                                VerticalOptions="StartAndExpand"
                                HeightRequest="210">
                        <CollectionView.ItemsLayout>
                            <LinearItemsLayout Orientation="Vertical"/>
                        </CollectionView.ItemsLayout>
                        <CollectionView.ItemTemplate>
                            <DataTemplate>
                            <ViewCell>
                                <Grid Margin="8,0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="auto"/>
                                    </Grid.RowDefinitions>
                                  <sh:Shadows Margin="0,0,0,5" 
                Shades="{sh:SingleShade Opacity=0.4, BlurRadius=10, Color=Black}"  CornerRadius="100">
                                        <Frame Grid.Column="0" BackgroundColor="#D9D9D9" HorizontalOptions="Center" VerticalOptions="Center" Padding="0" CornerRadius="100" WidthRequest="150" HeightRequest="150" IsClippedToBounds="True">
                                            <Image x:Name="userImage"  Source="{Binding Picture, Converter={StaticResource ByteArrayToImageConverter}}" WidthRequest="{OnPlatform iOS=100, Android=100}" HorizontalOptions="Center"/>
                                        </Frame>
                                    </sh:Shadows>
                                    <StackLayout Grid.Column="1" Margin="10,0,0,0">
                                        <Label Text="{Binding Name, TargetNullValue='Sconosciuto'}" FontFamily="Inter" FontAttributes="Bold" MaxLines="3" LineBreakMode="TailTruncation" FontSize="16" HorizontalOptions="Start" TextColor="Black" Margin="0,0,10,0"/>
                                        <Label Text="{Binding Address, TargetNullValue='Sconosciuto'}"  FontFamily="Inter" FontSize="13" HorizontalOptions="Start" TextColor="#D9D9D9"/>
                                    </StackLayout>
                                </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
                    </CollectionView>

这就是我在视图模型中加载集合视图“库”的方式:

public LoanBookPageViewModel(Book book) {
            Book = book;
            Libraries = BookService.GetLibrariesWithAvailableBook(Book);
        }

库中正确填充了对象,所以这不是问题。

xamarin.forms casting uicollectionview
1个回答
0
投票

是的,如果我们在

ViewCell
内部添加
CollectionView
元素,将会抛出以下错误。

 System.InvalidCastException:`Special cast is not valid`

因此,尝试删除

ViewCell
中的
CollectionView
元素。

正如 Jason 提到的,

ViewCell
中将需要元素
ListView

例如:

       <ListView ItemsSource="{Binding Items }">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                  <Label Text="{Binding Id}"/>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
© www.soinside.com 2019 - 2024. All rights reserved.