【问题标题】:Xamarin.Forms: bind to a code behind property in XAMLXamarin.Forms:绑定到 XAML 中的代码隐藏属性
【发布时间】:2019-06-07 19:18:00
【问题描述】:

在 Xamarin.Forms 中,我想将代码隐藏属性绑定到 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”属性绑定到标签,仅使用 XAML,这意味着无需在后面的代码中设置绑定或绑定上下文。

这可能吗?

【问题讨论】:

    标签: c# xaml xamarin.forms


    【解决方案1】:

    你的页面需要实现INotifyPropertyChanged,但是绑定语法应该是

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

    【讨论】:

    • 谢谢杰森,它成功了!我不需要实现INotifyPropertyChanged,因为ContentPage 继承自BindableObject,它已经实现了。
    • BindingContext="{x:Reference MyPage}" 或者
    • 注意:如果Label 不需要将任何属性绑定到 VM,则此答案很方便。如果您将来可能将绑定属性添加到同一标签,但这些新属性需要绑定到 VM(而不是代码隐藏),请参阅 goroth's answer,它仅将 Text 属性绑定到代码隐藏。
    【解决方案2】:

    只需在代码隐藏文件中添加BindingContext = this;

    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 LblText}" />
                </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();
            BindingContext = this;
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
            this.LblText = "Ciao!!";
        }
    }
    

    【讨论】:

      【解决方案3】:

      或者,如果您不想为整个控件交换绑定上下文,请使用以下内容:

      <?xml version="1.0" encoding="utf-8" ?>
      <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   x:Name="this"
                   x:Class="SomePage">
          <ContentPage.Content>
                  <StackLayout>
                      <Label Text="{Binding SomeProp, Source={x:Reference this}}" />
                  </StackLayout>
          </ContentPage.Content>
      </ContentPage>
      

      【讨论】:

      • 我认为,这是最有价值的答案。在基于现代 MVVM 模式的应用程序中,您几乎永远不会绑定到背后的控件代码,并且您不想交换真实的数据上下文 (ViewModel)。我正在寻找 {Binding ElementName=this, Path=BlaBla} 的 WPF 等效项,就是这样。这也适用于 Xamarin ContentView。
      • 另见the earlier accepted answer。如果 Label 需要 multiple 绑定到代码隐藏成员,并且不需要绑定到 VM 中的值,则接受的答案很方便。如果Label 还可以将其他属性绑定到 VM 中的值,则此答案是最好的。
      • XamlParseException: 找不到 this 引用的对象
      【解决方案4】:

      您需要按照 Jason's Answer 所述设置 ContentPage 的 x:Name。

      <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"
                   x:Name = "MyControl"/>
      

      您可以使用 ElementName 来代替 BindingContext

       <TextBlock Text="{Binding ElementName=TestControl,Path=StudentName}"/>
      

      【讨论】:

      • Xamarin.Forms XAML 语法为:&lt;TextBlock Text="{Binding Source={Reference TestControl}, Path=StudentName}"/&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 2012-07-15
      • 2017-01-28
      • 2011-07-04
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多