c# - Xamarin.Forms:绑定(bind)到 XAML 中的代码隐藏属性

标签 c# xaml xamarin.forms

在 Xamarin.Forms 中,我想将代码隐藏属性绑定(bind)到 XAML 中的标签。

我找到了很多关于这个主题的答案和网页,但它们都涵盖了更复杂的场景。

这是我的 XAML 页面:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TrackValigie"
             x:Class="TrackValigie.SelViaggioPage">
    <ContentPage.Content>
            <StackLayout>
                <Label Text="{Binding ?????????}" />
            </StackLayout>
    </ContentPage.Content>
</ContentPage>

这是隐藏的代码:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SelViaggioPage : ContentPage
{

    private string _lblText;
    public string LblText
    {
        get
        {
            return _lblText;
        }
        set
        {
            _lblText = value;
            OnPropertyChanged();
        }
    }

    public SelViaggioPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {

        this.LblText = "Ciao!!";

        base.OnAppearing();
    }
}

我想将“LblText”属性绑定(bind)到标签,仅使用 XAML,这意味着无需在代码隐藏中设置绑定(bind)或绑定(bind)上下文。

这可能吗?

最佳答案

您的页面需要实现INotifyPropertyChanged,但绑定(bind)语法应该是

<ContentPage x:Name="MyPage" ... />

... 

<Label BindingContext="{x:Reference Name=MyPage}" Text="{Binding LblText}" />

关于c# - Xamarin.Forms:绑定(bind)到 XAML 中的代码隐藏属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54170720/

相关文章:

c# - 打印没有 GridLines 的 WPF 网格

c# - 另一个未更新的 MVVM 可见性属性

c# - 导航问题 : PopToRootAsync is not supported globally on iOS, 请使用 NavigationPage

c# - 如何向 Visual Studio 扩展添加额外的工具窗口?

c# - 使用数据库日期时间

wcf - 无法解析符号 'Point'、 'Size'、 'PointCollection'

c# - Xamarin Android 6.0 Java.Lang.NoSuchMethodError 与 View.SetText

c# - Xamarin.forms 在 windows8 模拟器中给出 "invalid cross thread access"异常

c# - 从 asp 页面调用 C# 函数

xaml - 将可见元素添加到 Silverlight 3 中的自定义面板