C# 在mono上更改Winforms数据绑定和InotifyProperty
我已经制作了我的第一个mono应用程序,它运行在raspberry pi上。问题是数据绑定没有更新UI。更具体地说,我的控制器/模型中的PropertyChanged事件为null。这意味着没有订户 当我在VisualStudio调试器中的windows上运行应用程序时,ui会得到正确更新 单声道版本:4.6.2 OS:拉斯宾喘息 .NET:4.5 我没有发现关于这种情况的太多信息。由于它在windows上工作,并且mono支持INotifyPropertyChanged接口,因此我假设它也将在linux上的mono中运行C# 在mono上更改Winforms数据绑定和InotifyProperty,c#,winforms,mono,monodevelop,inotifypropertychanged,C#,Winforms,Mono,Monodevelop,Inotifypropertychanged,我已经制作了我的第一个mono应用程序,它运行在raspberry pi上。问题是数据绑定没有更新UI。更具体地说,我的控制器/模型中的PropertyChanged事件为null。这意味着没有订户 当我在VisualStudio调试器中的windows上运行应用程序时,ui会得到正确更新 单声道版本:4.6.2 OS:拉斯宾喘息 .NET:4.5 我没有发现关于这种情况的太多信息。由于它在windows上工作,并且mono支持INotifyPropertyChanged接口,因此我假设它也将在
//在代码中创建绑定
dhtControl.labelName.DataBindings.Add(“Text”,dht,“Name”);
我认为不需要其他代码,因为它是默认的INotifyPropertyChanged实现。唯一的区别是我向模型传递了一个操作(control.Invoke)来调用主线程上的更新
关于我有同样的问题,解决了添加ViewModel触发的操作事件,其中更新所有控件:
internal void InvokeUIControl(Action action)
{
// Call the provided action on the UI Thread using Control.Invoke() does not work in MONO
//this.Invoke(action);
// do it manually
this.lblTemp.Invoke(new Action(() => this.lblTemp.Text = ((MainViewModel)(((Delegate)(action)).Target)).Temperature));
this.lblTime.Invoke(new Action(() => this.lblTime.Text = ((MainViewModel)(((Delegate)(action)).Target)).Clock));
}
我注意到了.NET和Mono之间的差异,我也遇到了同样的问题。在比较.NET和Mono源代码之后,首先出现的情况是,如果您希望在ViewForm中收到propertyName通知,则必须在模型中首先更改任何控件.text:
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler TextChanged;
protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
if (propertyName == "Text")
{
TextChanged?.Invoke(this, EventArgs.Empty);
}
}
}
将事件处理程序命名为“TextChanged”以通知TextChanged是很重要的。
然后仍然在模型中,可以设置:
private string _Text = "";
public string Text {
get {
return _Text;
}
set {
if (_Text != value) {
NotifyPropertyChanged ("Text");
}
}
}
现在,在你看来,你可以这样做
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace EventStringTest
{
public partial class Form1 : Form
{
Model md = Model.Instance;
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add("Text", md, "Text", false
, DataSourceUpdateMode.OnPropertyChanged);
this.OnBindingContextChanged(EventArgs.Empty);
textBox1.TextChanged += (object sender, System.EventArgs e) => { };
}
private void Form1_Load(object sender, EventArgs evt)
{
md.PropertyChanged += (object s, PropertyChangedEventArgs e) => { };
// This is just to start make Text Changed in Model.
md.TimerGO ();
}
}
}
这似乎有很多代码,但我仍在寻找一个更好的解决方案。你最终找到了吗?我也遇到了同样的问题。我发现在添加数据绑定时,它们不会使用Mono注册到PropertyChangedEvent,而是使用.Net注册。还是不知道为什么。