【问题标题】:Simple Xamarin Forms text binding in code behind not working代码中的简单 Xamarin Forms 文本绑定不起作用
【发布时间】:2017-08-15 09:51:32
【问题描述】:

我有一个非常简单的代码示例来尝试测试数据绑定的一个方面。但是,我无法让它工作,我希望有人能指出我做错了什么。

在cs代码隐藏文件中我有这个:

string textSample = "This is a test!";
BindingContext = this;

在 xaml 文件中:

<Label Text = "{Binding Path=textSample}" />

当我运行代码时,标签不显示任何内容。

我显然遗漏了什么,但我看不到什么。

提前感谢您的帮助

【问题讨论】:

    标签: xaml xamarin


    【解决方案1】:

    您只能绑定到公共properties

    public string textSample { get { return "This is a test!"; }};
    BindingContext = this;
    

    Path 不需要:

    <Label Text = "{Binding textSample}" />
    

    【讨论】:

      【解决方案2】:

      另一种方式

      public string textSample {get;set;} = "This is a test!";
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 2017-08-18
        • 2020-08-06
        • 2020-08-29
        • 1970-01-01
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        相关资源
        最近更新 更多