2014-09-23 33 views
2

有沒有辦法在事件發生後更改編輯器單元格中的文本?Xamarin.Forms刷新編輯器的TextProperty

我有一個編輯器單元格顯示從SQLite數據庫的地址。我也有一個獲取當前地址的按鈕,並在提示中詢問他們是否想要更新地址。如果是,那麼我想在編輯器單元格中顯示新地址。

public class UserInfo : INotifyPropertyChanged 
{ 
    public string address; 
    public string Address 
    { 
     get { return address; } 
     set 
     { 
      if (value.Equals(address, StringComparison.Ordinal)) 
      { 
       return; 
      } 
      address = value; 
      OnPropertyChanged(); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

我對編輯單元代碼是

Editor userAddress = new Editor 
{ 
    BindingContext = uInfo, // have also tried uInfo.Address here 
    Text = uInfo.Address, 
    Keyboard = Keyboard.Text, 

};

,然後在此之後它已得到當前地址我有這個

bool response = await DisplayAlert("Current Address", "Would you like to use this as your address?\n" + currAddress, "No", "Yes"); 
    if (response) 
    { 
     //we will update the editor to show the current address 
     uInfo.Address = currAddress; 
    } 

我如何得到它來更新編輯單元格以顯示新的地址?

回答

2

您正在設置控件的BindingContext,但沒有指定綁定來處理它。您想要將編輯器的TextProperty綁定到上下文的Address屬性。

Editor userAddress = new Editor 
{ 
    BindingContext = uinfo, 
    Keyboard = Keyboard.Text 
}; 

// bind the TextProperty of the Editor to the Address property of your context 
userAddress.SetBinding (Editor.TextProperty, "Address"); 

這也可以工作,但我不肯定的語法是正確的:

Editor userAddress = new Editor 
{ 
    BindingContext = uinfo, 
    Text = new Binding("Address"), 
    Keyboard = Keyboard.Text 
}; 
+0

感謝您的快速回復。當我使用userAddress.SetBinding(Editor.TextProperty,「地址」);我得到一個System.InvalidCastException:無法從源類型轉換爲目標類型 – user1667474 2014-09-23 04:12:59

+0

我非常確定該語法是正確的。如果你看看Xamarin ToDo示例,應該有一個使用相同綁定方法的編輯器控件。 – Jason 2014-09-23 04:34:31

+0

是的,它和ToDo一樣,但是它會在頁面加載時拋出異常。我剛剛試過這個 - userAddress.SetBinding(Editor.TextProperty,新的綁定(「地址」,BindingMode.TwoWay)),這仍然不會更新編輯器,但它不會拋出異常或S - 我想我越來越接近 – user1667474 2014-09-23 05:23:40