简体   繁体   English

Xamarin Forms 绑定到嵌套属性不起作用

[英]Xamarin Forms binding to nested property not working

In addition to the Microsoft docs, I've been referencing this SO question: Xamarin Forms Databinding "."除了 Microsoft 文档之外,我还参考了这个 SO 问题: Xamarin Forms 数据绑定“。” separator 分隔器

The 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:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:local="clr-namespace:NAS.App.Framework"
    x:Class="NAS.App.Pages.Complications"
    ControlTemplate="{StaticResource ContentPageTemplate}">
    <ContentPage.Content>
        <StackLayout>
            <CheckBox IsChecked="{Binding InnerProperty1.NestedValue}" />
            <CheckBox IsChecked="{Binding InnerProperty1Checked}" />
            <Label Text="{Binding InnerProperty1.NestedLabel}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

The code behind:后面的代码:

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace NAS.App.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Complications : ContentPage
    {
        public Complications()
        {
            InitializeComponent();
            InnerProperty1 = new NestedProperty("InnerProperty1");

            BindingContext = this;
        }

        public bool InnerProperty1Checked
        {
            get { return InnerProperty1.NestedValue; }
            set { InnerProperty1.NestedValue = value; }
        }

        public NestedProperty InnerProperty1;
    }

    public class NestedProperty
    {
        public NestedProperty(string label)
        {
            NestedLabel = label;
        }

        private bool _value;
        public bool NestedValue
        {
            get { return _value; }
            set
            {
                _value = value;
            }
        }

        public string NestedLabel { get; private set; }
    }
}

Based on MS docs and the referenced article, I would expect the Label element, bound to InnerProperty1.NestedLabel to display the text "InnerProperty1" (as passed to the NestedProperty constructor).根据 MS 文档和参考文章,我希望 Label 元素绑定到 InnerProperty1.NestedLabel 以显示文本“InnerProperty1”(传递给 NestedProperty 构造函数)。 The Label element displays nothing - the binding isn't working. Label 元素不显示任何内容 - 绑定不起作用。

I would also expect the NestedProperty.NestedValue's setter to get called when I check or uncheck the first Checkbox element.我还希望在我选中或取消选中第一个 Checkbox 元素时调用 NestedProperty.NestedValue 的设置器。 A breakpoint on the "_value = value;" “_value = value;”上的断点line is not hit.线不打。 The binding isn't working here, either.绑定在这里也不起作用。

I wrap the InnerProperty with a direct property, InnerPropert1Checked, and bind the second Checkbox element to that.我使用直接属性 InnerPropert1Checked 包装 InnerProperty,并将第二个 Checkbox 元素绑定到该属性。 The breakpoint is hit when I check / uncheck the second Checkbox.当我选中/取消选中第二个复选框时,断点被击中。 The binding to the non-nested property does work.绑定到非嵌套属性确实有效。

I can't come up with an explanation as to why binding to the nested properties isn't working.我无法解释为什么绑定到嵌套属性不起作用。

Xamarin Forms 4.6.0.726 Xamarin Forms 4.6.0.726

Also, the page is the ContentPresenter portion of a ControlTemplate that's defined in App.xaml.此外,该页面是 App.xaml 中定义的 ControlTemplate 的 ContentPresenter 部分。 And the page is a ShellContent element in AppShell.xaml.该页面是 AppShell.xaml 中的 ShellContent 元素。 Given that non-nested binding is working, I'm not sure how either of those would come into play.鉴于非嵌套绑定正在工作,我不确定其中任何一个将如何发挥作用。 But maybe I'm missing something there.但也许我在那里遗漏了一些东西。

Any thoughts?有什么想法吗? Thank you.谢谢你。

you can only bind to public properties您只能绑定到公共属性

InnerProperty1 is a field, not a property InnerProperty1是一个字段,而不是一个属性

public NestedProperty InnerProperty1;

add a get to make it a property添加get使其成为属性

public NestedProperty InnerProperty1 { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM