将文本添加到 Xamarin.forms 中的绑定值

Add text to bound values in Xamarin.forms

我的模型视图:

public string Status {
    get { return _status; }
    set {
        if (value == _status) {
            return;
        }
        _status = value;
        OnPropertyChanged ("Status");
    }

我的看法:

Label labelStatus = new Label { 
    TextColor = Color.Green,
    FontSize = 20d
    };
    labelStatus.SetBinding (Label.TextProperty, "Status");

然后我想使用类似的方式显示状态:

string presentStatus = string.Format("Your status is {0}...", labelStatus);
Label yourStatus = new Label{Text=presentStatus}

但这实际上行不通。 using

也不行
string presentStatus = string.Format("Your status is {0}...", SetBinding(Label.TextProperty,"Status"));

那么在将绑定值显示给视图中的用户之前,我应该如何添加带有更多文本的绑定值。

如果使用 XAML(我不使用),似乎可以根据:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/data_binding_basics/

Xamarin Forms 绑定实现目前不允许复杂的绑定场景,例如在静态文本中嵌入绑定文本。

有两种选择

一个。使用多个标签 - 一个带有静态文本,一个带有绑定文本

b。在您的 ViewModel 上使用 属性 为您连接文本

public string StatusText 
{   
  get
  {
      return string.Format("Your status is {0}...", Status);   
  }
}


public string Status {
    get { return _status; }
    set {
        if (value == _status) {
            return;
        }
        _status = value;
        OnPropertyChanged ("Status");
        OnPropertyChanged ("StatusText");
    }

您可以在 BindingContextChanged 事件中执行此操作:

labelStatus.BindingContextChanged += (sender, e) =>
{
// Here you can change the Text dynamically
// E.G. labelStatus.text = "Title: " + labelStatus.text
};