【问题标题】:Add text to bound values in Xamarin.forms将文本添加到 Xamarin.forms 中的绑定值
【发布时间】:2015-08-08 09:42:22
【问题描述】:

我的模型视图:

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}

但这并没有真正起作用。也不用

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/ 似乎是可能的

【问题讨论】:

  • 你在任何地方设置 BindingContext 吗?
  • 是的,仅显示 labelstatus 就可以了,但是当我尝试在其周围添加文本时,它会失败。

标签: binding xamarin.forms


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以在 BindingContextChanged-event 中做到这一点:

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

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 2018-09-14
      相关资源
      最近更新 更多