c# - Xamarin Forms PCL with Caliburn,只有创建 View 层次结构的原始线程才能触及其 View

标签 c# android mvvm xamarin.forms caliburn.micro

在我的 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/

相关文章:

c# - 从 CSV 执行 SQL INSERT 时如何避免重复数据

c# - 为什么会调用 OnlyOnCanceled 延续?

android - 调用 soap wsdl web 服务 android

android - 如何在android中一起使用ffmpeg进行文本和gif叠加

c# - 如何在 MVVM-WPF 中获取选定的项目

c# - 这个 viewModel 是如何创建的?

c# - 如何在本地存储中创建、存储和访问 fileopen picker 的图像

c# - 如何播放正在编码的视频?

java - 图像显示不正确

c# - 将 WPF 应用程序转换为 MVVM 时必须知道的事情的一般 list