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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.