Forum Cross Platform with Xamarin

Frame in listView iOS issue

Hi,
Have a frame as a first child in a ListView, but when debugging on a simulator in iOS, it give me a System.NullReferenceExeption. When add for example a StackLayout around the frame, I get not this error.
Another issue what I have is with this frame in this ListView, have a ItemTapped event in the ListView, on iOS I can't tap a ListView item. How to achieve this.
On Android have no issue with this all.

Spec:
Visual studio 2019: 16.6.2
Package: Xamarin.Forms 4.7.0.968

GMinnow

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited August 2020

    Have a frame as a first child in a ListView, but when debugging on a simulator in iOS, it give me a System.NullReferenceExeption. When add for example a StackLayout around the frame, I get not this error.

    Could you show the code ?

    Another issue what I have is with this frame in this ListView, have a ItemTapped event in the ListView, on iOS I can't tap a ListView item. How to achieve this.

    Use ItemSelected or ItemTapped event ,they are triggered when tapping on the item , another way is adding TapGestureRecognizer on Frame ,you could handle the logic in Tapped event.

  • GMinnowGMinnow NLMember ✭✭

    Here is my Xaml code, Have used so you see a ItemTapped Event, but the error comes with navigating to this page.

    <ScrollView >
            <ListView 
             x:Name="ListViewContent" 
                      HasUnevenRows="True"
                      ItemTapped="ListViewContent_ItemTapped"                  
                      SeparatorVisibility="None"
                      BackgroundColor="#EFF5F3">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >                        
                            <!--STACKLAYOUT BEFORE FRAME IS NEEDED FOR IOS-->
                            <StackLayout 
                                Spacing="5"
                                BackgroundColor="WhiteSmoke"
                                Margin="5,5">
                                <Frame 
                                    CornerRadius="10"
                                    BackgroundColor="WhiteSmoke"
                                    HasShadow="True"
                                    Padding="0"
                                    Margin="10,5">                               
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="*" />
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="2*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Image   
                                            Source="{Binding ImageDir}" 
                                            Aspect="AspectFit" 
                                            HeightRequest="50" 
                                            WidthRequest="50"                                        
                                            Margin="5,10"
                                            VerticalOptions="Center"
                                            Grid.Column="0"
                                            Grid.Row="0"
                                            Grid.RowSpan="2">
                                        <Image.Clip>
                                            <EllipseGeometry Center="30,30" RadiusX="30" RadiusY="35"  />
                                        </Image.Clip>
                                            <Image.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                                            </Image.GestureRecognizers>
                                        </Image>
                                        <Label 
                                            Text="{Binding ItemName}" 
                                            TextColor="Black"                                         
                                            FontSize="Medium"
                                            FontAttributes="Bold"                                        
                                            Margin="0,5"
                                            Grid.Column="1"
                                            Grid.Row="0"/>
                                        <Label 
                                            Text="{Binding DescriptionItem}" 
                                            x:Name="textLabel"
                                            FontSize="Small"
                                            TextColor="DarkGray"
                                            LineBreakMode="WordWrap"
                                            Grid.Column="1"
                                            Grid.Row="1"
                                            Grid.ColumnSpan="2"/>
                                        <Label 
                                            Text="{Binding DateAndTime}" 
                                            TextColor="DarkGray"
                                            FontSize="Micro" 
                                            Grid.Column="2"
                                            Grid.Row="0"
                                            VerticalOptions="End"/>
                                        <Label 
                                            Text="{Binding TimeAndDate}"
                                            TextColor="DarkGray"
                                            FontSize="Micro"
                                            Grid.Column="2"
                                            Grid.Row="1"
                                            VerticalOptions="Start"/>
                                    </Grid>                               
                                </Frame>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollView>
    
  • ColeXColeX Member, Xamarin Team Xamurai

    Please show the code in method ListViewContent_ItemTapped .

  • ColeXColeX Member, Xamarin Team Xamurai

    I test to use Frame(without Stacklayout) as child of listview , no error happens.

    If possible could you post a basic ,minimal project here so that i can reproduce your issue .

  • GMinnowGMinnow NLMember ✭✭
    edited August 2020

    Hi CodeX,

    The error is not coming with the ListItemTapped event but when open de page with the listtems, and that's appends with this code.

    `protected async override void OnAppearing()
            {
                base.OnAppearing();
                try
                {
                    using (SQLiteConnection conn = new SQLiteConnection(App.DataBaseLocation))
                    {
                        conn.CreateTable<FoundITInfo>();
                        var info = conn.Table<FoundITInfo>().ToList();
    
                    if (info.Count != 0)
                    {
    
                        ListViewContent.ItemsSource = info;
                    }
    
                    else
                    {
                        await DisplayAlert("No Items", "There are no stored items. \n This app goes back to the Mainpage", "OK");
                        await Navigation.PushAsync(new MainPage());
                    }
                }
    
            }
            catch (Exception)
            {
    
                await DisplayAlert("Error", "Something went wrong", "OK");
            }
    
    
        }`
    

    In this app you can create items, and they will be stored in de Sqlite Database. When clicking on a item with the code below, you can decide what you want, for example extra info.

    `private async void ListViewContent_ItemTapped(object sender, ItemTappedEventArgs e)
            {
                try
                {
                    selectedItem = e.Item as FoundITInfo;
                    if (selectedItem != null)
                    {
                        await Navigation.PushAsync(new DecideMaps(selectedItem));
                    }
                }
                catch (Exception ex)
                {
    
                    await DisplayAlert("Failed", ex.Message, "OK");
                }
    
            }`
    
  • ColeXColeX Member, Xamarin Team Xamurai

    What is the exact error message ?

  • GMinnowGMinnow NLMember ✭✭

    Hi ColeX,
    Have updated the Xamarin.forms package to 4.8 and I don't now what's happening but get not this error anymore.
    Thanks for helping with this.
    What other issue what I talked about

    Another issue what I have is with this frame in this ListView, have a ItemTapped event in the ListView, on iOS I can't tap a ListView item.

    Have added Tap.GestureRecognizer in my frame but how I get the values of the clicked listItem

  • ColeXColeX Member, Xamarin Team Xamurai

    Something like

    private void TapGestureRecognizer_Tapped(object sender, System.EventArgs e)
    {
    
        var frame = sender as Frame;
        var model = frame.BindingContext as Model;
    }
    
  • GMinnowGMinnow NLMember ✭✭

    It's working. Thank you so much, :)

Sign In or Register to comment.