在我的 Xamarin Forms PCL 中,我使用 Caliburn.Micro.Xamarin.Forms 来实现 MVVM 数据绑定(bind)。项目的结构有一个 viewModel 和一个 View ,数据绑定(bind)是这样完成的:
在 View 模型中
private BindableCollection<TempListItem> _temperatures;
public BindableCollection<TempListItem> Temperatures
{
get { return _temperatures; }
set
{
if (value != _temperatures)
{
_temperatures = value;
NotifyOfPropertyChange(() => Temperatures);
}
}
}
在 View 中
var t2Name = new Label
{
FontSize = 17,
TextColor = (Color)Application.Current.Resources["ForegroundColor"]
};
t2Name.SetBinding(Label.TextProperty, new Binding("Temperatures[1].Name"));
我有这个问题
only the original thread that created a view hierarchy can touch its views
当我导航到另一个页面时会出现这种情况,我想这与数据绑定(bind)有关。_navigationService.For<MainViewModel>().Navigate();
最佳答案
我已经通过调用“绘制”UI 的主线程中的调用解决了这个问题。
例如,在单击按钮事件处理程序中:
private void btnSarasa_Clicked(object sender, EventArgs e)
{
Device.BeginInvokeOnMainThread(
() => { lblStatus.Text = "Sarasa"; }
);
}
或者在另一个事件处理程序中: private void Ws_OnOpened()
{
Device.BeginInvokeOnMainThread(
() =>
{
ws.Send(@"{""protocol"":""json"",""version"":1}" + recordSeparator);
lblStatus.Text = "WS abierto";
ws.Send("{\"arguments\":[\"" + "444" + "-1\"],\"invocationId\":\"" + "0" + "\",\"target\":\"JoinGroup\",\"type\":1}" + recordSeparator);
}
);
}
关于c# - Xamarin Forms PCL with Caliburn,只有创建 View 层次结构的原始线程才能触及其 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38185330/