如何从代码隐藏文件连续刷新网格的内容
How to refresh the content of a grid continiously from the code behind file
我尝试使用并行线程连续刷新网格的内容。那是不起作用的代码:
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);
}
if 子句有效,所以页面应该只在我的数据集发生变化时刷新(数据集由 CheckRequirements-Method 返回)
代码不工作:它在发生变化时进入 if 子句,但它没有初始化和显示新页面。
我认为这根本不是最佳做法,我想就如何做得更好提出建议。
更新UI操作应该在主线程中执行。尽量将相关的功能代码放在主线程中。如:
private void ContiniouslyRefreshPage(int interval)
{
...
MainThread.BeginInvokeOnMainThread(() =>
{
Application.Current.MainPage = new MasterDetail
{
Detail = new NavigationPage(new TestingPage())
{
BarBackgroundColor = Color.White,
BarTextColor = Color.Black
}
};
};
}
我尝试使用并行线程连续刷新网格的内容。那是不起作用的代码:
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);
}
if 子句有效,所以页面应该只在我的数据集发生变化时刷新(数据集由 CheckRequirements-Method 返回)
代码不工作:它在发生变化时进入 if 子句,但它没有初始化和显示新页面。
我认为这根本不是最佳做法,我想就如何做得更好提出建议。
更新UI操作应该在主线程中执行。尽量将相关的功能代码放在主线程中。如:
private void ContiniouslyRefreshPage(int interval)
{
...
MainThread.BeginInvokeOnMainThread(() =>
{
Application.Current.MainPage = new MasterDetail
{
Detail = new NavigationPage(new TestingPage())
{
BarBackgroundColor = Color.White,
BarTextColor = Color.Black
}
};
};
}