除了Microsoft文档之外,我还一直在参考以下SO问题:Xamarin Forms Databinding "." separator
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>
背后的代码:
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; }
}
}
基于MS docs和所引用的文章,我希望绑定到InnerProperty1.NestedLabel的Label元素显示文本“ InnerProperty1”(传递给NestedProperty构造函数)。 Label元素不显示任何内容-绑定无效。
我还希望在我选中或取消选中第一个Checkbox元素时调用NestedProperty.NestedValue的setter。 “ _value = value;”上的断点行未命中。绑定在这里也不起作用。
我用一个直接属性InnerPropert1Checked包装了InnerProperty,并将第二个Checkbox元素绑定到该属性。当我选中/取消选中第二个Checkbox时,会遇到断点。绑定到非嵌套属性确实有效。
我无法解释为何无法绑定到嵌套属性。
Xamarin表格4.6.0.726
此外,页面是App.xaml中定义的ControlTemplate的ContentPresenter部分。该页面是AppShell.xaml中的ShellContent元素。鉴于非嵌套绑定正在工作,所以我不确定这两个都将如何发挥作用。但是也许我在那里缺少什么。
有什么想法吗?谢谢。
答案 0 :(得分:4)
您只能绑定到公共属性
InnerProperty1
是一个字段,而不是一个属性
public NestedProperty InnerProperty1;
添加一个get
使其成为属性
public NestedProperty InnerProperty1 { get; set; }