Android 如何从代码隐藏文件连续刷新网格内容
我尝试用并行线程不断刷新网格的内容。这就是不起作用的代码:Android 如何从代码隐藏文件连续刷新网格内容,android,xamarin,xamarin.forms,cross-platform,Android,Xamarin,Xamarin.forms,Cross Platform,我尝试用并行线程不断刷新网格的内容。这就是不起作用的代码: private void ContiniouslyRefreshPage(int interval) { var startTimeSpan = TimeSpan.Zero; var periodTimeSpan = TimeSpan.FromSeconds(interval); Dictionary<string, st
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);
}
private void ContiniouslyRefreshPage(整数间隔)
{
var startTimeSpan=时间跨度0;
var periodTimeSpan=TimeSpan.FromSeconds(间隔);
Dictionary lastCheck=bluetoothService.CheckRequirements();
var timer=新系统.线程.计时器((e)=>
{
Dictionary newCheck=bluetoothService.CheckRequirements();
如果(!(lastCheck.Count==newCheck.Count&&!bluetoothService.CheckRequirements()。除了(lastCheck.Any()))
{
Application.Current.MainPage=新的主详细信息
{
详细信息=新建导航页面(新建测试页面())
{
BarBackgroundColor=颜色。白色,
BarTextColor=颜色。黑色
}
};
lastCheck=新支票;
}
},null,startTimeSpan,periodTimeSpan);
}
if子句起作用,因此只有当我的数据集发生更改时,页面才应该引用(数据集由CheckRequirements方法返回)
代码不起作用:当发生更改时,它会进入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
}
};
};
}
为什么只为了修改单个页面的内容而替换整个页面堆栈?因为只有创建视图的线程才能修改它。因此,我不能打开一个新的线程,每3秒钟检查一次更改和访问“主”线程创建的视图。非常感谢!更新UI操作应该在主线程中执行。尝试将相关函数代码放在主线程中。例如:
MainThread.beginInvokeMainThread(()=>{//在主线程上运行的代码})代码>。谢谢!只需提一下:@Jason批评加载整个新页面是正确的。我刚刚调用了一个方法,该方法在主线程内更改网格中的值。