C# 如何将标签文本绑定到代码隐藏中的字符串属性?

C# 如何将标签文本绑定到代码隐藏中的字符串属性?,c#,android,xamarin,data-binding,C#,Android,Xamarin,Data Binding,我看了所有我能找到的关于这个问题的帖子,没有一篇是以我能理解的方式回答这个问题的。我正在慢慢地尝试建立一个应用程序,跟踪纸牌游戏的生活。现在,我只是想让总寿命显示出来,这样我就可以为两个递增和递减按钮定义方法来完成它们的工作。我以前做过后端工作,所以我知道如何让按钮完成它们的工作,但我不明白为什么我的生活总字符串没有显示为文本 以下是xml代码: <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmln

我看了所有我能找到的关于这个问题的帖子,没有一篇是以我能理解的方式回答这个问题的。我正在慢慢地尝试建立一个应用程序,跟踪纸牌游戏的生活。现在,我只是想让总寿命显示出来,这样我就可以为两个递增和递减按钮定义方法来完成它们的工作。我以前做过后端工作,所以我知道如何让按钮完成它们的工作,但我不明白为什么我的生活总字符串没有显示为文本

以下是xml代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Notes.MainPage">
<StackLayout Margin="10,35,10,10">
    <Label 

           Text="{Binding lifeTotalString}"
           FontSize="30"
           HorizontalOptions="Center"
           FontAttributes="Bold"/>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button Text="-"
                Clicked="btnDecrement" />
        <Button Grid.Column="1"
                Text="+"
                Clicked="btnIncrement"/>
    </Grid>
</StackLayout>

}

您应该绑定到代码隐藏中的
BindableProperty
,并正确设置
BindingContext

public partial class MainPage : ContentPage
{
    int lifeTotal = 20;

    public static readonly BindableProperty lifeTotalStringProperty = BindableProperty.Create(
            nameof(lifeTotalString),
            typeof(string),
            typeof(MainPage),
            default(string));

    public string lifeTotalString
    {
        get => (string)GetValue(lifeTotalStringProperty);
        set => SetValue(lifeTotalStringProperty, value);
    }

    public MainPage()
    {
        InitializeComponent();
        lifeTotalString = lifeTotal.ToString();

        BindingContext = this;
    }

    void btnDecrement(object sender, EventArgs e)
    {
        lifeTotal--;

        lifeTotalString = lifeTotal.ToString();
    }

    void btnIncrement(object sender, EventArgs e)
    {
        lifeTotal++;

        lifeTotalString = lifeTotal.ToString();
    }
}
数据绑定连接两个对象的属性,称为源和 目标。在代码中,需要两个步骤:BindingContext 必须将目标对象的属性设置为源对象,并且 设置绑定方法(通常与绑定一起使用) 类)来绑定该对象的属性 对象设置为源对象的属性

目标属性必须是可绑定属性,这意味着 目标对象必须从BindableObject派生


您可以阅读文档了解更多详细信息:您是否尝试过我的解决方案?
public partial class MainPage : ContentPage
{
    int lifeTotal = 20;

    public static readonly BindableProperty lifeTotalStringProperty = BindableProperty.Create(
            nameof(lifeTotalString),
            typeof(string),
            typeof(MainPage),
            default(string));

    public string lifeTotalString
    {
        get => (string)GetValue(lifeTotalStringProperty);
        set => SetValue(lifeTotalStringProperty, value);
    }

    public MainPage()
    {
        InitializeComponent();
        lifeTotalString = lifeTotal.ToString();

        BindingContext = this;
    }

    void btnDecrement(object sender, EventArgs e)
    {
        lifeTotal--;

        lifeTotalString = lifeTotal.ToString();
    }

    void btnIncrement(object sender, EventArgs e)
    {
        lifeTotal++;

        lifeTotalString = lifeTotal.ToString();
    }
}