在 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/