android - 如何从文件后面的代码中不断刷新网格的内容
问题描述
我尝试使用并行线程不断刷新网格的内容。那是不工作的代码:
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
}
};
};
}
推荐阅读
- vba - 使用 VBA 将扫描值(从条形码扫描仪)移动到 excel 中的特定单元格
- spring - Spring Cloud slueth SpanAccessor 接口
- excel - 使用该范围内单元格的内容在活动范围内添加公式
- html - 单击表格行发生冲突
- linux - 从所有文件中删除关闭 PHP 标记
- python - 如何在 Dask DataFrame 中按列分组并将 agg 作为列表的另一列?
- java - Java 中的数组:java.lang.ArrayIndexOutOfBoundsException
- angular - 基于每个点的值的具有渐变的颜色点
- php - PHP - preg_replace_callback 因子标签而失败
- php - 如何使用 php xmlreader 读取 xml 文件