2017-02-12 57 views
3

我已經使我的第一個單聲道應用程序運行在覆盆子pi上。問題是數據綁定沒有更新UI。更具體地說,我的控制器/模型中的PropertyChanged事件爲null。這意味着沒有用戶。Winforms數據綁定和單聲道INotifyPropertyChanged

當我在Visual Studio調試器的windows上運行應用程序時,ui得到了正確更新。

單聲道版本:4.6.2 OS:Raspbian喘息 .NET 4.5

我發現在該方案中沒有太多的信息。由於它在windows和mono上工作,支持INotifyPropertyChanged接口,所以我認爲它也可以在Linux上以單聲道運行。

// creating the binding in code dhtControl.labelName.DataBindings.Add("Text", dht, "Name");

我覺得這是不需要其他的代碼,因爲它是默認的INotifyPropertyChanged的實現。唯一的區別是我將一個Action(control.Invoke)傳遞給模型以調用主線程上的更新。

問候

+0

你最終搞清楚了嗎?我遇到了同樣的問題。 – Kohanz

+0

我發現在添加數據綁定時,他們沒有使用Mono註冊到PropertyChangedEvent,但他們使用.Net。仍然不知道爲什麼。 – soulsource

回答

0

我有同樣的問題,解決了加入由視圖模型,其中更新所有的控制發射一個動作事件:

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));   
    } 
0

我注意到.NET和Mono和我之間的差異有同樣的問題。比較.NET和Mono的源代碼後,它首先出現的是,如果你想在ViewForm收到通知propertyName的任何Control.TextChanged你首先要在你的模型:

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(); 
     } 
    } 
} 

這似乎很多代碼,但我仍然在尋找一個更優雅的更好的解決方案。