xamarin 形式的标签的数据绑定部分 [英] Data Binding part of a label in xamarin forms

查看:26
本文介绍了xamarin 形式的标签的数据绑定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Xamarin 形式的标签.我必须显示一个基本上是一个句子的文本,但该字符串的一部分包含一个我从 api 调用中获得的数字,并且字符串的其余部分是固定的.

I am using Label in Xamarin forms. I got to display a text which is basically a sentence but part of that string contains a number which I get from an api call and rest of the string is fixed.

我想使用数据绑定来设置该部分.示例:

I want to use data binding to set that part. Example :

文本可能是:你肯定能赢 {0} 美元"

Text could be like : "You can win {0} dollars for sure"

{0} 值来自 api 并且想使用数据绑定来绑定它.

{0} value comes from api and want to use data binding to bind it.

需要用于绑定这种字符串的语法.

Need the syntax to be used to bind this kind of string.

推荐答案

您可以使用模型绑定到标签中的数据.就像这样:在 xaml 中:

You can use modle bind to data in your label.Just like this: in xaml :

 <Label Text="{Binding Name,StringFormat='You can win {0} dollars for sure'}" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />

在 ContentPage 中,应该绑定上下文:

in ContentPage, should bind context:

 nativeDataTemple = new NativeDataTemple();
 BindingContext = nativeDataTemple;

和 Molde(NativeDataTemple 你自定义) 应该包含绑定属性,像这样:

and Molde(NativeDataTemple you custom) should contain the binding property,like this:

private string name = "520";
    public string Name
    {
        set
        {
            if (name != value)
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }
        get
        {
            return name;
        }
    }

并且在您的模型中,当名称值在后台更改时,将 INotifyPropertyChanged 添加到模型和方法

and in your modle ,when Name value change in the background,add INotifyPropertyChanged to the modle,and method

 protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

然后你想改变数据的地方,就这样做:

then where you want change the data ,jusst do that:

nativeDataTemple.Name = "550";

如果有问题,你可以参考这个官方文档

if have problem ,you can refer to thisOfficial document

这篇关于xamarin 形式的标签的数据绑定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆