Xamarin数据绑定到当前属性

时间:2018-09-17 07:55:20

标签: xamarin data-binding properties

我正在尝试将一些字符串添加到current.properties中,但是不确定如何正确地进行操作。

现在我正在使用绑定创建一个Label,然后将label.text放入属性中,但似乎有点愚蠢:

var userLabel = new Label {};
userLabel.SetBinding(Label.TextProperty, "name");

Application.Current.Properties["studentName"] = userLabel.Text; 

期待更简单的方法;-)

1 个答案:

答案 0 :(得分:0)

这是一种解决方法:

function axiosTest() {
  return axios.get('http://localhost:3001/Drug/' + id).then((response) => {
        return response.data["0"].drugName,
    });
}

axiosTest().then(data => {
  response.json({ message: 'Request received!', data })
})

设置class MyViewModel : INotifyPropertyChanged { string text; public event PropertyChangedEventHandler PropertyChanged; public MyViewModel() { Text = "York"; } public string Text { set { if (text != value) { text = value; OnPropertyChanged("Text"); Application.Current.Properties["Text"] = text; } } get { return text; } } protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }

BindingContext

在您的protected override void OnAppearing() { base.OnAppearing(); MyViewModel mainPageViewModel = new MyViewModel(); BindingContext = mainPageViewModel; } 中使用它:

label

有关更多详细信息,请参阅:Xamarin Forms Data Binding Basics

相关问题