Xamarin表示数据绑定"." 分隔器

Sac*_*h K 11 data-binding xamarin.forms

我正在为Xamarin Forms中的数据绑定而苦苦挣扎.这就是为什么,我期望从以下XAML语句中发生的事情:

IsVisible="{Binding Path=UserContext.IsLoggedOut}"
Run Code Online (Sandbox Code Playgroud)

是 - 该属性绑定到视图模型的子对象.现在要么在Xamarin Forms中不支持,要么我错过了一个技巧.

如果在支持WPF的Xamarin中不支持,那么我们应该做些什么来传播嵌套对象?展平视图模型正在让我写出令人难以置信的代码和大量代码.

Sve*_*übe 17

支持嵌套属性,就像其他非常复杂的表达式一样:

你可以测试一下:

XAML

<StackLayout Spacing="20">
  <StackLayout Orientation="Horizontal">
    <Label Text="Subproperties: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.SubItem.Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
  <StackLayout Orientation="Horizontal">
    <Label Text="Indexer: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.Dictionary[key].Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
  <StackLayout Orientation="Horizontal">
    <Label Text="Array Indexer: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.Array[1].Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
</StackLayout>
Run Code Online (Sandbox Code Playgroud)

public partial class Page2 : ContentPage
{
    public ItemModel Item { get; }

    public Page2()
    {
        InitializeComponent();
        Item = new ItemModel();
        BindingContext = this;

    }
}

public class ItemModel
{
    public ItemSubModel SubItem { get; set; }
    public Dictionary<string, ItemSubModel>  Dictionary { get; set; }
    public ItemSubModel[] Array { get; set; }

    public ItemModel()
    {
        SubItem = new ItemSubModel();
        Dictionary = new Dictionary<string, ItemSubModel>
        {
            {"key", new ItemSubModel()}
        };
        Array = new [] {new ItemSubModel(), new ItemSubModel() };
    }
}

public class ItemSubModel
{
    public string Text { get; set; } = "Supported";
}
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述