UWP:After listview binds data, it flashes when updating data.

Hank Mody 21 Reputation points
2021-08-23T06:29:58.527+00:00

XAML code(UWP):

  <Grid>
    <StackPanel Orientation="Horizontal">
        <ListView ItemsSource="{x:Bind results}" x:Name="list">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding name}"/>
                        <ListView ItemsSource="{Binding re}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <ListView ItemsSource="{x:Bind items_list}" x:Name="RESULT">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding cont}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</Grid>

The C# code:

    using System;
    using System.Collections;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Timers;
    using Windows.UI.Xaml.Controls;
 namespace test
 {
   public sealed partial class MainPage : Page
  {
    private Timer timer;
    private ObservableCollection<result> results = new ObservableCollection<result>();
    private ObservableCollection<Item> items_list = new ObservableCollection<Item>();
    public MainPage()
    {
        this.InitializeComponent();
        timer = new Timer();
        timer.Interval = 200;
        timer.Elapsed += Timer_Elapsed;
        timer.Start();
        items_list.Add(new Item() { Name = "1", cont = 1 });
    }
    private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        int ii = 0;
        int Max = 5;
        var re0a = new result() { re=new ArrayList(){1,1,1} };
        await RESULT.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            foreach (Item i in items_list)
            {
                if (Max >= 10)
                {
                    if (re0a.re.Count < (ii + 1))
                        re0a.re.Add("1");
                    else
                        re0a.re[ii] = 2; 
                }
                if (results.Count < (ii + 1))
                    results.Add(re0a);
                else
                    results[ii] = re0a;
                ii++;
            }
        });

    }
    public class Item
    {
        public string Name { get; set; } = null;

        public int cont { get; set; } = 0;
    }
    public class result
    {
        public string name { get; set; } = null;
        public ArrayList re { get; set; } = null;
    }
}

}

When updating data, listview will keep flashing.What can be done to solve this problem?

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Answer accepted by question author
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,871 Reputation points
    2021-08-24T02:25:24.35+00:00

    Hello, Welcome to Micorosoft Q&A,

    UWP:After listview binds data, it flashes when updating data.

    The problem is you set the sub listview ItemsSource directly, but not update the different one, for this scenario, the better way is modify the ArrayList as ObservableCollection and update the sub-listview with new datasouce.

    private async void Timer_Elapsed(object sender, ElapsedEventArgs e)  
    {  
        int ii = 0;  
        int Max = 5;  
        var re0a = new result() { re = new ObservableCollection<int> () { 1, 2, 3 } };  
        await RESULT.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
        {  
            foreach (Item item in items_list)  
            {  
                if (Max >= 10)  
                {  
                    if (re0a.re.Count < (ii + 1))  
                        re0a.re.Add(1);  
                    else  
                        re0a.re[ii] = 2;  
                }  
                if (results.Count < (ii + 1))  
                    results.Add(re0a);  
                else  
      
                results[ii].name = re0a.name;  
                var diffe0 = re0a.re.Where(c => !results[ii].re.Any(p => p == c)).ToArray();  
                 
                foreach (var df in diffe0)  
                {  
                    results[ii].re.Add(df);  
                }  
                 
                ii++;  
            }  
        });  
      
    }  
    public class Item  
    {  
        public string Name { get; set; } = null;  
        public int cont { get; set; } = 0;  
    }  
    public class result : INotifyPropertyChanged  
    {  
        private string _name;  
        public string name  
        {  
            get => _name;  
            set  
            {  
                _name = value;  
                OnPropertyChanged();  
            }  
        }          
        public ObservableCollection<int> re { set; get; }  
        public event PropertyChangedEventHandler PropertyChanged;  
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)  
        {  
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
        }  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.