Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Label in viewcell disappearing when updating listview's itemssource on locked Android device

$
0
0

I am running into a bug with labels in my viewcells. I managed to capture this behaviour in the following test app:

C# code:

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace AndroidLockscreenIssue
{
    public partial class AndroidLockscreenIssuePage : ContentPage
    {
        public AndroidLockscreenIssuePage()
        {
            InitializeComponent();

            list1.ItemsSource = new List<Item>
            {
                new Item("Item title 1", "Item detail 1"),
                new Item("Item title 2", "Item detail 2"),
                new Item("Item title 3", "Item detail 3"),
            };
            list2.ItemsSource = list1.ItemsSource;
        }

        /// <summary>
        /// Trigger bugging
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        public void OnClick(object sender, EventArgs e){
            StartTimer();
            bugbtn.IsEnabled = false;
        }

        /// <summary>
        /// Reset list sources> 
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        public void OnFixClick(object sender, EventArgs e)
        {
            var source = list1.ItemsSource;
            list1.ItemsSource = null;
            list1.ItemsSource = source;
            list2.ItemsSource = null;
            list2.ItemsSource = source;
            fixbtn.IsEnabled = false;
            bugbtn.IsEnabled = true;
        }

        /// <summary>
        /// Shuffle the list a bit in 5 seconds.
        /// </summary>
        public void StartTimer()
        {
            Device.StartTimer(TimeSpan.FromSeconds(5), delegate
            {
                Device.BeginInvokeOnMainThread(delegate
                {
                    var coll = ((List<Item>)list1.ItemsSource);
                    var firstitem = coll[0];
                    coll.RemoveAt(0);
                    coll.Insert(coll.Count-1, firstitem);
                    list1.ItemsSource = null;
                    list1.ItemsSource = coll;
                    list2.ItemsSource = null;
                    list2.ItemsSource = coll;
                    System.Diagnostics.Debug.WriteLine("TIMER: updating list collection");
                    fixbtn.IsEnabled = true;
                });
                return false;
            });
        }
    }

    public class Item
    {
        public string Text { get; set; }
        public string Detail { get; set; }
        public Item(string text, string detail)
        {
            Text = text;
            Detail = detail;
        }
    }
}

xaml code:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AndroidLockscreenIssue" x:Class="AndroidLockscreenIssue.AndroidLockscreenIssuePage">
    <StackLayout>
        <Label Text="Press the 'Bug it!' button to trigger a list change in 5 seconds. Lock your screen, wait 5+ seconds and unlock your screen. The list will be 'empty'." VerticalOptions="Center" HorizontalOptions="Center" />
        <Button Text="Bug it!" Clicked="OnClick" x:Name="bugbtn"/>
        <Button Text="Fix it!" Clicked="OnFixClick" x:Name="fixbtn" IsEnabled="false"/>
        <Label Text="Bugged list (ViewCell)"/>
        <ListView x:Name="list1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Text}" FontSize="Large"/>
                            <Label Text="{Binding Detail}"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Label Text="OK list (TextCell)"/>
        <ListView x:Name="list2">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Text}" Detail="{Binding Detail}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

When I press the 'bug it!' button, it triggers a timer that will change the itemssource within 5 seconds. If you lock your android device before this update happens and unlock after it happened, the list with ViewCells will not render the labels whilst the list with TextCells does render it's text just fine. Is there any way to fix this?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>