让我们在ReactiveUI for WinForms中有一个简单的绑定。
using System;
using System.Threading;
using System.Windows.Forms;
using ReactiveUI;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form, IViewFor<ViewModel>
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
ViewModel = new ViewModel();
this.Bind(ViewModel, vm => vm.Text, v => v.Text);
//BeginInvoke(new Action(() => ViewModel.Text = "WinForm context thread text"));
ThreadPool.QueueUserWorkItem(state => ViewModel.Text = "Cross-thread text");
}
object IViewFor.ViewModel
{
get
{
return ViewModel;
}
set
{
ViewModel = (ViewModel) value;
}
}
public ViewModel ViewModel { get; set; }
}
public class ViewModel: ReactiveObject
{
private string _text;
public string Text
{
get
{
return _text;
}
set
{
this.RaiseAndSetIfChanged(ref _text, value);
}
}
}
}
在 OnLoad()方法中设置ViewModel.Text属性会生成 InvalidOperationException ,其中跨线程操作无效:控制&#39; Form1&#39;从创建它的线程以外的线程访问。文本。 有没有办法告诉ReactiveUI自动同步呼叫?我想避免在ViewModel类中进行同步调用...