PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 13, 2022

[FIXED] How to refresh the content of a grid continiously from the code behind file

 September 13, 2022     android, cross-platform, xamarin, xamarin.forms     No comments   

Issue

I try to refresh the content of my grid continiously with a parallel thread. Thats the code which is not working:

private void ContiniouslyRefreshPage(int interval)
        {
            var startTimeSpan = TimeSpan.Zero;
            var periodTimeSpan = TimeSpan.FromSeconds(interval);           
            Dictionary<string, string> lastCheck = bluetoothService.CheckRequirements();
            var timer = new System.Threading.Timer((e) =>
            {
                Dictionary<string, string> newCheck = bluetoothService.CheckRequirements();
                if (!(lastCheck.Count == newCheck.Count && !bluetoothService.CheckRequirements().Except(lastCheck).Any()))
                {
                    Application.Current.MainPage = new MasterDetail
                    {
                        Detail = new NavigationPage(new TestingPage())
                        {
                            BarBackgroundColor = Color.White,
                            BarTextColor = Color.Black
                        }
                    };
                    lastCheck = newCheck;
                }
            }, null, startTimeSpan, periodTimeSpan);
        }

The if-clause works, so the page should only refesh when there is a change of my dataset (dataset is returned by CheckRequirements-Method)

The code is not working: It gets into the if-clause when there is a change but it doesn't initialize and display the new Page.

I think this is not the best practise at all, i'd like to have an advice how to do it better.


Solution

The updating UI opeation should be executed in the main thread. Try to put the related function code in the main thread. Such as:

private void ContiniouslyRefreshPage(int interval)
{
    ...
    MainThread.BeginInvokeOnMainThread(() =>
    {
        Application.Current.MainPage = new MasterDetail
        {
            Detail = new NavigationPage(new TestingPage())
            {
                BarBackgroundColor = Color.White,
                BarTextColor = Color.Black
            }
        };
    };
}


Answered By - Jarvan Zhang
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing